0% found this document useful (0 votes)
4 views30 pages

Array Operations: Pseudocode & Java

The document contains a series of pseudocode and corresponding Java code snippets for various programming tasks, including array manipulation, mathematical calculations, and string operations. Each task is presented with its pseudocode followed by a Java implementation. The tasks cover topics such as finding sums, averages, maximum values, and checking conditions like even/odd and palindrome.

Uploaded by

giveandgetbazaar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views30 pages

Array Operations: Pseudocode & Java

The document contains a series of pseudocode and corresponding Java code snippets for various programming tasks, including array manipulation, mathematical calculations, and string operations. Each task is presented with its pseudocode followed by a Java implementation. The tasks cover topics such as finding sums, averages, maximum values, and checking conditions like even/odd and palindrome.

Uploaded by

giveandgetbazaar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Write pseudocode to initialize an integer array of size 5 and populate it with user input.

Pseudocode:

DECLARE array[5]

FOR i FROM 0 TO 4

INPUT "Enter a number: ", array[i]

END FOR

```

Java Code:

public class ArrayInput {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

int[] array = new int[5];

for (int i = 0; i < 5; i++) {

[Link]("Enter a number: ");

array[i] = [Link]();

[Link]();

```
2. Write pseudocode to find the sum of all elements in an integer array.

Pseudocode:

DECLARE array[5]

DECLARE sum = 0

FOR i FROM 0 TO 4

sum ← sum + array[i]

END FOR

OUTPUT "Sum: ", sum

```

Java Code:

public class ArraySum {

public static void main(String[] args) {

int[] array = {1, 2, 3, 4, 5};

int sum = 0;

for (int i = 0; i < [Link]; i++) {

sum += array[i];

[Link]("Sum: " + sum);

}
```

3. Write pseudocode to check if a given number is present in an integer array.

Pseudocode:

DECLARE array[5]

DECLARE target

DECLARE found = FALSE

INPUT "Enter a number to find: ", target

FOR i FROM 0 TO 4

IF array[i] = target THEN

found ← TRUE

EXIT FOR

END IF

END FOR

IF found THEN

OUTPUT target, " is present in the array."

ELSE

OUTPUT target, " is not present in the array."

END IF

```
Java Code:

import [Link];

public class FindNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

int[] array = {1, 2, 3, 4, 5};

[Link]("Enter a number to find: ");

int target = [Link]();

boolean found = false;

for (int i = 0; i < [Link]; i++) {

if (array[i] == target) {

found = true;

break;

if (found) {

[Link](target + " is present in the array.");

} else {

[Link](target + " is not present in the array.");

}
[Link]();

```

4. Write pseudocode to find the maximum value in a floating-point array.

Pseudocode:

```

DECLARE floatArray[5]

DECLARE max = floatArray[0]

FOR i FROM 1 TO 4

IF floatArray[i] > max THEN

max ← floatArray[i]

END IF

END FOR

OUTPUT "Maximum value: ", max

```
Java Code:

public class MaxFloat {

public static void main(String[] args) {

float[] floatArray = {1.2f, 3.5f, 2.8f, 4.1f, 2.0f};

float max = floatArray[0];

for (int i = 1; i < [Link]; i++) {

if (floatArray[i] > max) {

max = floatArray[i];

[Link]("Maximum value: " + max);

```

5. Write pseudocode to count the number of even elements in an integer array.

Pseudocode:

DECLARE intArray[5]

DECLARE count = 0

FOR i FROM 0 TO 4

IF intArray[i] MOD 2 = 0 THEN


count ← count + 1

END IF

END FOR

OUTPUT "Number of even elements: ", count

```

Java Code:

public class CountEven {

public static void main(String[] args) {

int[] intArray = {2, 7, 4, 9, 6};

int count = 0;

for (int i = 0; i < [Link]; i++) {

if (intArray[i] % 2 == 0) {

count++;

[Link]("Number of even elements: " + count);

```

6. Write pseudocode to find the average of elements in a floating-point array.


Pseudocode:

```

DECLARE floatArray[5]

DECLARE sum = 0

DECLARE average

FOR i FROM 0 TO 4

sum ← sum + floatArray[i]

END FOR

average ← sum / 5

OUTPUT "Average: ", average

```

Java Code:

public class ArrayAverage {

public static void main(String[] args) {

float[] floatArray = {1.2f, 3.5f, 2.8f, 4.1f, 2.0f};

float sum = 0;

float average;

for (int i = 0; i < [Link]; i++) {

sum += floatArray[i];

}
average = sum / [Link];

[Link]("Average: " + average);

```

[Link] pseudocode to find the factorial of a given number.

Pseudocode:

DECLARE number

DECLARE factorial = 1

INPUT "Enter a number: ", number

FOR i FROM 1 TO number

factorial ← factorial * i

END FOR

OUTPUT "Factorial: ", factorial

```

Java Code:

public class Factorial {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");

int number = [Link]();

int factorial = 1;

for (int i = 1; i <= number; i++) {

factorial *= i;

[Link]("Factorial: " + factorial);

```

8. Write pseudocode to check if a given string is a palindrome.

Pseudocode:

DECLARE inputString

DECLARE isPalindrome = TRUE

DECLARE length = LENGTH(inputString)

FOR i FROM 0 TO length / 2

IF inputString[i] ≠ inputString[length - 1 - i] THEN


isPalindrome ← FALSE

EXIT FOR

END IF

END FOR

IF isPalindrome THEN

OUTPUT inputString, " is a palindrome."

ELSE

OUTPUT inputString, " is not a palindrome."

END IF

```

Java Code:

public class PalindromeCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a string: ");

String inputString = [Link]();

boolean isPalindrome = true;

int length = [Link]();

for (int i = 0; i < length / 2; i++) {

if ([Link](i) != [Link](length - 1 - i)) {

isPalindrome = false;
break;

if (isPalindrome) {

[Link](inputString + " is a palindrome.");

} else {

[Link](inputString + " is not a palindrome.");

```

9. Write pseudocode to remove duplicate elements from an integer array.

Pseudocode:

DECLARE intArray[10]

DECLARE uniqueArray[]

DECLARE index = 0

FOR i FROM 0 TO 9

IF NOT intArray[i] IN uniqueArray THEN

uniqueArray[index] ← intArray[i]

index ← index + 1

END IF
END FOR

OUTPUT "Unique elements: ", uniqueArray

```

Java Code:

import [Link];

public class RemoveDuplicates {

public static void main(String[] args) {

int[] intArray = {1, 2, 3, 2, 4, 5, 6, 7, 7, 8};

int[] uniqueArray = new int[10];

int index = 0;

for (int i = 0; i < 10; i++) {

if (!contains(uniqueArray, intArray[i])) {

uniqueArray[index] = intArray[i];

index++;

[Link]("Unique elements: " + [Link](uniqueArray));

private static boolean contains(int[] array, int target) {

for (int value : array) {


if (value == target) {

return true;

return false;

```

10. Write pseudocode to find the common elements between two integer arrays.

Pseudocode:

```

DECLARE array1[5]

DECLARE array2[5]

DECLARE commonArray[]

DECLARE index = 0

FOR i FROM 0 TO 4

FOR j FROM 0 TO 4

IF array1[i] = array2[j] THEN

commonArray[index] ← array1[i]

index ← index + 1

END IF

END FOR

END FOR
OUTPUT "Common elements: ", commonArray

```

Java Code:

import [Link];

public class CommonElements {

public static void main(String[] args) {

int[] array1 = {1, 2, 3, 4, 5};

int[] array2 = {3, 4, 5, 6, 7};

int[] commonArray = new int[5];

int index = 0;

for (int i = 0; i < 5; i++) {

for (int j = 0; j < 5; j++) {

if (array1[i] == array2[j]) {

commonArray[index] = array1[i];

index++;

[Link]("Common elements: " + [Link](commonArray));

```
11. Write pseudocode to calculate the average of three numbers.

Pseudocode:

```

DECLARE num1, num2, num3

DECLARE average

INPUT "Enter first number: ", num1

INPUT "Enter second number: ", num2

INPUT "Enter third number: ", num3

average ← (num1 + num2 + num3) / 3

OUTPUT "Average: ", average

```

Java Code:

public class AverageCalculation {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter first number: ");

double num1 = [Link]();

[Link]("Enter second number: ");

double num2 = [Link]();


[Link]("Enter third number: ");

double num3 = [Link]();

double average = (num1 + num2 + num3) / 3;

[Link]("Average: " + average);

```

12. Write pseudocode to check if a number is positive, negative, or zero.

Pseudocode:

DECLARE number

INPUT "Enter a number: ", number

IF number > 0 THEN

OUTPUT number, " is positive."

ELSE IF number < 0 THEN

OUTPUT number, " is negative."

ELSE

OUTPUT number, " is zero."

END IF
```

Java Code:

public class PositiveNegativeZero {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");

int number = [Link]();

if (number > 0) {

[Link](number + " is positive.");

} else if (number < 0) {

[Link](number + " is negative.");

} else {

[Link](number + " is zero.");

```
13. Write pseudocode to find the square of a given number.

Pseudocode:

DECLARE number

DECLARE square

INPUT "Enter a number: ", number

square ← number * number

OUTPUT "Square: ", square

```

Java Code:

public class SquareCalculation {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");

double number = [Link]();

double square = number * number;

[Link]("Square: " + square);


}

```

14. Write pseudocode to determine if a year is a leap year.

Pseudocode:

DECLARE year

INPUT "Enter a year: ", year

IF (year MOD 4 = 0 AND year MOD 100 ≠ 0) OR (year MOD 400 = 0) THEN

OUTPUT year, " is a leap year."

ELSE

OUTPUT year, " is not a leap year."

END IF

```

Java Code:

public class LeapYearCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a year: ");

int year = [Link]();


if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

[Link](year + " is a leap year.");

} else {

[Link](year + " is not a leap year.");

```

15. Write pseudocode to calculate the area of a rectangle.

Pseudocode:

DECLARE length, width

DECLARE area

INPUT "Enter length: ", length

INPUT "Enter width: ", width

area ← length * width

OUTPUT "Area: ", area

```
Java Code:

public class RectangleArea {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter length: ");

double length = [Link]();

[Link]("Enter width: ");

double width = [Link]();

double area = length * width;

[Link]("Area: " + area);

```

16. Write pseudocode to check if a given character is a vowel.

Pseudocode:

DECLARE character

INPUT "Enter a character: ", character


IF character IS ONE OF 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' THEN

OUTPUT character, " is a vowel."

ELSE

OUTPUT character, " is not a vowel."

END IF

```

Java Code:

public class VowelCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a character: ");

char character = [Link]().charAt(0);

if ("aeiouAEIOU".indexOf(character) != -1) {

[Link](character

+ " is a vowel.");

} else {

[Link](character + " is not a vowel.");

```
17. Write pseudocode to find the maximum of two numbers.

Pseudocode:

DECLARE num1, num2

DECLARE max

INPUT "Enter first number: ", num1

INPUT "Enter second number: ", num2

IF num1 > num2 THEN

max ← num1

ELSE

max ← num2

END IF

OUTPUT "Maximum: ", max

```

Java Code:

public class MaxOfTwo {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter first number: ");

double num1 = [Link]();


[Link]("Enter second number: ");

double num2 = [Link]();

double max = (num1 > num2) ? num1 : num2;

[Link]("Maximum: " + max);

```

18. Write pseudocode to determine if a given number is even or odd.

Pseudocode:

DECLARE number

INPUT "Enter a number: ", number

IF number MOD 2 = 0 THEN

OUTPUT number, " is even."

ELSE

OUTPUT number, " is odd."

END IF

```
Java Code:

public class EvenOddCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");

int number = [Link]();

if (number % 2 == 0) {

[Link](number + " is even.");

} else {

[Link](number + " is odd.");

```

19. Write pseudocode to find the minimum of three numbers.

Pseudocode:

DECLARE num1, num2, num3

DECLARE min

INPUT "Enter first number: ", num1


INPUT "Enter second number: ", num2

INPUT "Enter third number: ", num3

min ← num1

IF num2 < min THEN

min ← num2

END IF

IF num3 < min THEN

min ← num3

END IF

OUTPUT "Minimum: ", min

```

Java Code:

public class MinOfThree {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter first number: ");

double num1 = [Link]();

[Link]("Enter second number: ");

double num2 = [Link]();


[Link]("Enter third number: ");

double num3 = [Link]();

double min = num1;

if (num2 < min) {

min = num2;

if (num3 < min) {

min = num3;

[Link]("Minimum: " + min);

```

20. Write pseudocode to check if a number is a prime number.

Pseudocode:

DECLARE number

DECLARE isPrime = TRUE


INPUT "Enter a number: ", number

IF number ≤ 1 THEN

isPrime ← FALSE

ELSE

FOR i FROM 2 TO SQRT(number)

IF number MOD i = 0 THEN

isPrime ← FALSE

EXIT FOR

END IF

END FOR

END IF

IF isPrime THEN

OUTPUT number, " is a prime number."

ELSE

OUTPUT number, " is not a prime number."

END IF

```

Java Code:

public class PrimeCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a number: ");


int number = [Link]();

boolean isPrime = true;

if (number <= 1) {

isPrime = false;

} else {

for (int i = 2; i <= [Link](number); i++) {

if (number % i == 0) {

isPrime = false;

break;

if (isPrime) {

[Link](number + " is a prime number.");

} else {

[Link](number + " is not a prime number.");

```

Common questions

Powered by AI

To determine the maximum value in a floating-point array using pseudocode, first declare an array and a variable to hold the maximum value, initialized to the first element of the array. Then iterate over the array starting from the second element. For each element, compare it to the current maximum value, and if it's larger, update the maximum value. Continue this until the end of the array. Finally, output the maximum value. This step-by-step procedure ensures that the largest number is correctly identified .

The pseudocode for counting even elements in an integer array involves initializing a counter to zero, then using a loop to iterate over each element in the array. For each element, it checks if it is even by using the modulus operator with 2. If true, it increments the counter. After completing the loop, the counter reflects the total number of even elements, which is then output. This process is basic but effective for counting purposes .

The pseudocode for checking if a number is prime begins by initializing a flag variable as true. It first checks if the number is less than or equal to 1, setting the flag to false in such a case, as these numbers are not prime. Then it iteratively checks divisibility of the number starting from 2 up to the square root of the number. If any divisor is found, the flag is set to false. After the loop, the flag determines if the number was prime or not. This flow is well-suited to identify non-prime numbers efficiently .

Pseudocode calculates the average of elements in a floating-point array by initializing a sum variable to zero, iterating over each element to add its value to the sum, and then dividing the total sum by the number of elements. The result is assigned to an average variable which is then output. This logical sequence ensures each step maintains arithmetic integrity while handling float operations .

The pseudocode uses a simple conditional structure to determine if a number is even or odd by checking if the number modulo 2 equals zero. If true, the number is even; otherwise, it is odd. This approach is direct and leverages the standard definition of even numbers as those divisible by 2 without a remainder .

Pseudocode assists in removing duplicates by iterating over the array and checking if the current element is not in the unique array. If it isn't, it adds it to the unique array. This method is straightforward and easy to understand. However, its limitations include inefficiency for large arrays due to repeated scanning for duplicates, resulting in a high time complexity. Optimal implementations typically require more complex data structures, such as hashsets, to track existing entries more efficiently .

Pseudocode can be used to calculate the sum of elements in an array by iterating over each element and accumulator variable is updated with the current element. Specifically, you initialize a sum variable to zero and then use a loop to iterate over the array elements, adding each element to the sum. Finally, the sum is output. This approach provides a clear, step-by-step representation which directly translates into code structure .

Handling edge cases in leap year calculation pseudocode is crucial because leap years follow a complex rule-set that includes exceptions (years divisible by 100 must also be divisible by 400 to be leap years, though all divisible by 4 generally are). Properly coding these conditions ensures that years such as 1900 are not incorrectly marked as leap years, ensuring accurate and reliable calendar-related calculations .

Pseudocode verifies if a string is a palindrome by comparing characters from the beginning and end moving towards the middle. It initializes a boolean flag to true, then uses a loop to compare the ith character from the start with the ith character from the end for half the string length. If a mismatch is found, the flag is set to false, and the loop breaks. The final flag state determines the output. This step-by-step logic is effective in ensuring accurate palindrome detection .

Pseudocode contributes to understanding binary search as it encapsulates the logic of halving the search space each iteration, thus enhancing comprehension of efficiency gains from logarithmic time complexity. It requires proper preconditions (e.g., sorted array) and involves initializing low and high indices that converge. By using pseudocode, it becomes easier to visualize the recursive decision-making of choosing left or right sublists based on comparisons, demonstrating the concept analytically [No specific source cited].

You might also like