Write a java program to print all even and all odd numbers in an array.

 

Write a java program to print all even and  all odd numbers in an array. 

Program:


import java.util.*;
public class print_odd_even
{
public static void main (String args[])
{
int i,n;
Scanner sc=new Scanner(System.in);

System.out.println("Enter the  size  of an array: ");
n=sc.nextInt();

int a[]=new int[n];
System.out.println("Enter the  elements of an array: ");
for(i=0; i<n; i++)
{
a[i]=sc.nextInt();
}
System.out.println("even numbers are:");
for(i=0; i<n; i++)
{
if(a[i]%2==0)
System.out.println(a[i]);
}
System.out.println("odd numbers are:");
for(i=0; i<n; i++)
{
if(a[i]%2!=0)
System.out.println(a[i]);
}
}
}


Output :
Enter the  size  of an array:
5
Enter the  elements of an array:
25
6
5
10
14
even numbers are:
10
14
6
odd numbers are:
25
5

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java