How to find Even and Odd Number?

This java program we find that a number is even or odd. If the number is divisible by ‘2’ then it will be ‘Even’, otherwise it is ‘Odd’.

Step 1: Lets open a notepad and write the following code.

//Code for Even Number

class demo

{

public static void main(String arg[])

{

int x=60;

if(x%2==0)

{

System.out.println(“number is even”);

}

else

{

System.out.println(“number is odd”);

}

}

}

Step 2: Name it as “if.java” and save the file on any location. I saved at “C:\kiran\program”.

Step 3: Open command prompt (Press window + R and write cmd and hit OK).

Step 4: Set the path.

1

Step 5: Now write the following code for checking my java file is compiling or not.

javac if.java.

2

My java program compiled successfully.

 

Step 6: Write the following code in command prompt. Press enter and see the output.

java demo

// demo is a class name which is written in my if.java file.

Output:

3

Step 7: If we wants to find the odd number then see the code.

class demo

{

    public static void main(String arg[])

    {

                int x=169;

                if(x%2==0)

                {

                    System.out.println(“number is even”);

                }

                else

                {

                    System.out.println(“number is odd”);

                }

    }

}

Step 8: The Output is:

5

Tags: , ,