This is a Java Program to Print the Odd & Even Numbers in an Array.
Enter size of array and then enter all the elements of that array. Now using for loop and if codition we use to distinguish whether given integer in the array is odd or even.
Here is the source code of the Java Program to Print the Odd & Even Numbers in an Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Even_Odd
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Odd numbers:");
for(int i = 0 ; i < n ; i++)
{
if(a[i] % 2 != 0)
{
System.out.print(a[i]+" ");
}
}
System.out.println("");
System.out.print("Even numbers:");
for(int i = 0 ; i < n ; i++)
{
if(a[i] % 2 == 0)
{
System.out.print(a[i]+" ");
}
}
}
}
Output:
$ javac Even_Odd.java $ java Even_Odd Enter no. of elements you want in array:5 Enter all the elements: 1 2 3 4 5 Odd numbers:1 3 5 Even numbers:2 4
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Practice Information Technology MCQs
- Check Programming Books
- Practice BCA MCQs
- Apply for Java Internship
- Apply for Computer Science Internship