Answer to Question #282709 in Java | JSP | JSF for Meher

Question #282709

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 ...


1
Expert's answer
2021-12-26T09:41:29-0500
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();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog