Amazon Prime




 
Tagged
  • Java
  • Java Programs
  •  

    Java Program to Check Given Number is Even or Odd

    In this tutorial, we will learn how to check whether a number is even or odd.

    What is Even Number ?

    A number that is divisible by 2 and results a remainder of 0 is called an Even Number. Examples : 2, 4, 6, 8, ..., 98, 100 etc.

    What is Odd Number ?

    A number that is divisible by 2 and results a remainder of 1 is called Odd Number Examples : 1, 3, 5, 7, ..., 97, 99 etc.

    In java we use modulo operator (represented using % symbol) to determine the remainder.

    Algorithm

    1Step 1 : Start
    2Step 2 : Read number "n"
    3Step 3 : rem = n % 2 (Initialize remainder as rem)
    4Step 4 : If rem = 0, print Even
    5Step 5 : Else print Odd.
    6Step 6 : End

    Code Implementation

    1import java.util.Scanner;
    2
    3public class EvenOdd {
    4
    5 public static void main(String[] args) {
    6 Scanner sc = new Scanner(System.in);
    7 System.out.println("Enter Number :");
    8 int number = sc.nextInt();
    9
    10 int rem = number % 2 ;
    11 if (rem == 0) {
    12 System.out.printf("%d is Even\n", number);
    13 }else {
    14 System.out.printf("%d is Odd\n", number);
    15 }
    16 }
    17}

    Output :

    1Enter Number : 21
    221 is Odd
     

    Output :

    1Enter Number : 18
    218 is Even
     

  • Java
  • Java Programs
  •  
    ...