Question

Write a complete java program to read 10 integer numbers into an array named list. Then Use the array to output the odd ...


Write a complete java program to read 10 integer numbers into an array named list. Then Use the array to output the odd numbers followed by even numbers

Answer

Task : Write a Java program to take input of 10 integers and then print them in order mentioned in the question.

 

Java Program :- 

 

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] list = new int[10];
        System.out.println("Enter 10 integers : ");
        for(int i=0;i<10;i++) 
            list[i] = sc.nextInt();
        
        // first output odd integers
        System.out.print("Output : ");
        for(int i=0;i<10;i++) {
            if(list[i]%2 != 0)
                System.out.print(list[i] + " ");
        }

        // first output even integers 
        for(int i=0;i<10;i++) {
            if(list[i]%2 == 0)
                System.out.print(list[i] + " ");
        }
    }
}

Screenshot for the code and sample input/output :- 

 

Computer Engineering homework question answer, step 2, image 1

Recent Questions