Java Array Program to Print Odd Numbers in Array

By | July 4, 2019

Task: Write a Java Array Program to Print Odd Numbers in Array.

This Java Array program uses simple logic to check whether number is odd. It will divide a number with 2 and get remainder of division using % arithmetic operator. Therefore, if the remainder is a 1, the number will be odd.

"<yoastmark

The Source code for Java Array program to print odd numbers in array is as follows:

/*
 * Java Program To Input 10 integer numbers
 in array and print list of only odd numbers in array.
 */
package javaoddarray;
import java.util.Scanner;
/**
 * @author www.EasyCodebook.com
 */
public class JavaOddArray {

 public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a[]=new int[10],i;
        
        for(i=0;i<a.length ;i++)
        {
            System.out.print("Enter number in Array["+i+"]=");
            a[i]=input.nextInt();
        }
        System.out.println("List of Odd Numbers in Array is:");
        for(i=0;i<a.length ;i++)
        {
            if(a[i]%2==1)
              System.out.print(a[i]+", ");
        }
    }
    
}

A sample run and output of Java array program is as follows:

Enter number in Array[0]=11
Enter number in Array[1]=12
Enter number in Array[2]=13
Enter number in Array[3]=55
Enter number in Array[4]=40
Enter number in Array[5]=90
Enter number in Array[6]=100
Enter number in Array[7]=800
Enter number in Array[8]=95
Enter number in Array[9]=111
List of Odd Numbers in Array is:
11, 13, 55, 95, 111,

Here is a java program to show even numbers in array using for loop

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *