Write a program in java to store 100 to 150 in a Single dimension array then display all even numbers in one line in ascending order and all odd numbers in next line in descending order with appropriate heading as follows:
Even numbers are-
100 102 104 106 ...
Odd numbers are-
149 147 145 143 ...
public class Main {
public static void main(String[] args) {
int[] array = new int[51];
for (int i = 0, j = 100; i < array.length; i++, j++) {
array[i] = j;
}
System.out.println("Even numbers are-");
for (int i = 0; i < array.length; i++) {
if (array[i] % 2 == 0) {
System.out.print(array[i] + " ");
}
}
System.out.println("\nOdd numbers are-");
for (int i = array.length - 1; i >= 0; i--) {
if (array[i] % 2 != 0) {
System.out.print(array[i] + " ");
}
}
System.out.println();
}
}
Comments
Leave a comment