Ticker

6/recent/ticker-posts

Odd Even Program in JAVA

Program in JAVA to check whether a given number is odd or even


Logic 1 : By Checking Divisibility by 2


// Program to check whether the Number entered is Even or Odd
import java.util.Scanner;
public class odd {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Your Number here --> ");
        int number = in.nextInt();
        in.close();
        if (number % 2 == 0)
            System.out.println("Number is Even");
        else
            System.out.println("Number is Odd");
    }
}


Logic 2: By Dividing Number by 2 and if it is not exactly divisible by 2 then the number is odd else even


// Program to check whether the Number entered is Even or Odd
import java.util.Scanner;
public class odd {
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Your Number here --> ");
        int number = in.nextInt();
        in.close();
        if ((number / 2) * 2 == number)
            System.out.println("Number is Even");
        else
            System.out.println("Number is Odd");
    }
}

Logic 3: By comparing last digit of number with even number array


// Program to check whether the Number entered is Even or Odd

import java.util.Scanner;

public class even

{

    public static void main(String args[])

    {

        Scanner in = new Scanner(System.in);

        char EvenArray[] = { '0''2''4''6''8' };

        boolean NumberIsEven = false;

        System.out.print("Enter Your Number here --> ");

        String number = in.nextLine();

        in.close();

        char lastDigit = number.charAt(number.length() - 1);

        for (int i = 0i < 5i++)

            if (lastDigit == EvenArray[i])

                NumberIsEven = true;

        if (NumberIsEven)

            System.out.println("Even Number");

        else

            System.out.println("Odd Number");

    }

}



If you have any doubt related to Programs or if you want me to solve your problem or if you need logic of any program feel free to Comment.

Post a Comment

2 Comments

  1. Hey, your article is interesting. I will share with my friends. My colleagues were taking about cloudcouncil that they provide certification on Java. As you have knowledge in it can you help me is that worth studying and get certified ? https://cloudcouncil.org/

    ReplyDelete
    Replies
    1. Yes, It is worth studying JAVA. But knowledge is above certificate. If you have passion and think that you can gather new skills and knowledge then you should take a course.

      Delete