Monday 14 March 2016

Find out the given number as odd or even simple java coding

Coding for finding number of numbers as odd and even from a given array.


import java.io.*;
class oddeven
{
public static void main(String args[])throws IOException
{
DataInputStream s = new DataInputStream(System.in);
int n,i;
int odd,even;
odd=even=0;
int a[] = new int[100];
System.out.println("Enter the number of elements");
n= Integer.parseInt(s.readLine());
System.out.println("Enter the "+n+" numbers");
for(i=0;i<n;i++)
 {
  a[i]=Integer.parseInt(s.readLine());
  if(a[i]%2==0)
    {
    even++;
    }
  else
    {
        odd++;
    }
  }
System.out.println("Odd numbers are:" +odd);
System.out.println("Even numbers are:" +even);
}
}


Set the path and compile the program

C:\Documents and Settings\dcom358>H:

H:\>CD JAVA

H:\Java>path=C:\jdk1.3\bin;

H:\Java>javac oddeven.java
Note: oddeven.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.

H:\Java>java oddeven
Enter the number of elements
7
Enter the 7 numbers
12
10
13
15
17
18
25
Odd numbers are:4
Even numbers are:3

No comments:

Post a Comment