Friday, July 10, 2015

Even Odd Program in Java

  1. An even number is an integer which is "evenly divisible" by two. This means that if the integer is divided by 2, it yields no remainder. Zero is an even number because zero divided by two equals zero. Even numbers can be either positive or negative.
  2. An odd number is an integer of the form , where is an integer. The odd numbers are therefore ..., , , 1, 3, 5, 7, .... Integers which are not odd are called even. Odd numbers leave a remainder of 1 when divided by two, i.e., the congruence holds for odd .
  3. In this program first if condition will check if no. Is greater than 0 or not if yes than no. Will go inside the loop for operation if not than it will return to starting point. 
  4. Inside the loop if no. Will devide by 2 and remainder will be 0 than no. Is even . and if remainder will be 1 than no. Is odd.

import java.util.Scanner;

public class EvenOdd {


public static void main(String[] args)
{

Scanner scn=new Scanner (System.in);
System.out.println(" Type a no. to chech even or odd");
int n=scn.nextInt();
if(n>0)
{
if(n%2==0)
{
System.out.println("No. Is Even");
}
else
{
System.out.println("No.Is Odd");
}
}
else
{
System.out.println("Plese type a valid no. exepting 0");
}

}

}

0 comments:

Post a Comment