Monday, August 28, 2017

If-else statement, read input from keyboard (by java.util.Scanner) and Find if a number is Even Or Odd example in Java



/* this example explains three important topics of Java
1. read input from keyboard
2. if-else statement
3. calculate if the number is even or odd
  */

import java.util.Scanner;

public class FindEvenOrOddNumber {

        public static void main(String[] args) {
        // main() function

        Scanner keyboard = new Scanner(System.in);
        // Scanner object to read input from keyboard
        System.out.println("enter an integer");
        int even_odd = keyboard.nextInt();
         if(even_odd%2 == 0) //check if number is odd or even
                                System.out.println(even_odd + " is even number.");
                         else
                                System.out.println(even_odd + " is odd number.");
}

}

No comments:

Post a Comment