Skip to main content

Command Palette

Search for a command to run...

Java Programming : Odd or Even

Updated
1 min readView as Markdown
Java Programming : Odd or Even

Example

Input 
enter the number 5
Output
The number is odd

Required to Know

Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out.print(), Use of If-else, and Use of Data types.

Steps

  • Take the number as an input from the user.

  • Check if the number is divisible by 2 or not. If it is divisible print even. Otherwise, print odd.

Condition

if(num%2==0){
    print("even")
}
else{
    print("odd")
}

Program

import java.util.Scanner;

public class odd_even {

    /**
     * This class checks if a number is even or odd.
     */

    public static void main(String[] args) {

        // Create a Scanner object to read user input.
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter a number.
        System.out.print("Enter a number: ");
        int a = sc.nextInt();

        // Check if the number is divisible by 2.
        if (a % 2 == 0) {

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

        } else {

            // The number is odd.
            System.out.println("The number is odd");

        }

    }
}

More from this blog

TheCodingcoder

60 posts

Empowering the coding community by simplifying complex tasks.