Java even and odd | codeYourslf - coding on mobile phone.

Check even or odd number in java programming language with different approach. java even or odd. codeYourslf Coding on mobile. Java for beginners
2 min read

Java even odd program

Java even and odd | codeYourslf - coding on mobile phone.

How to write a java program that will determine whether a number is even or odd. I am writing the code in my mobile. 

Source code for java even and odd


package asicQuestion;

public class evenOdd {

    public static void main(String[] args){

      System.out.println("Enter the number to check even or odd");

      java.util.Scanner sc = new java.util.Scanner(System.in);

      int num = sc.nextInt();

      if(num == 0){

        System.out.println("The number is zero");

      }

      else if(num % 2 == 0 && num != 0){

        System.out.println("The number is even");

      }

      

      else{

        System.out.println("THE number is odd");

      }

    }

}

Output: 

Java even and odd | codeYourslf - coding on mobile phone.

Java even odd program using conditional ternary operator :- 


public class evenOddUsingTernary {

    public static void main(String[] args){

      int num = 11;

      System.out.println(num%2==0?"Even number":"Odd number");

    }

}

- The conditional ternary operator ( ? :), will work like if-else statement. 

(Condition) ? "It will execute, if condition true" : "It will execute, if condition false";

Logic for even odd in java :- 

- When we divide a number with 2 and if the remainder will be 0, then the number will be even. 

- Otherwise the number will be odd number.

Please check these topics :-

`

You may like these posts

Post a Comment

Hey Please 👏, feel free to share your opinion 🌍