In this Java program, you'll learn to read integer number from the console using Scanner. We will write the logic to check whether the number is even or odd.
Java Program to Check Even or Odd Number
package com.javaguides.java.tutorial;
import java.util.Scanner;
/**
* Java Program to check Even or Odd number
*
* @author https://www.sourcecodeexamples.net/
*
*/
public class JavaProgramEvenOddNumber {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
// Read number from user
System.out.println("Enter Number: ");
int number = sc.nextInt();
/*
* If number is divisible by 2 then it's an even number else odd number
*/
if (number % 2 == 0) {
System.out.println("The number " + number + " is even");
} else {
System.out.println("The number " + number + " is odd");
}
}
}
}
Output:
Enter Number:
10
The number 10 is even
Related Java Programs
- Java program to calculate the area of Triangle
- Java Program to Calculate Area of Square
- Java Program to Calculate Area of Rectangle
- Java Program to find the Smallest of three numbers using Ternary Operator
- Java Program to Find Largest of Three Numbers
- Java Program to Find GCD of Two Numbers
- Java Program to Check Armstrong Number
- Java Program to Generate Random Number
- Java Program to Check if Number is Positive or Negative
- Java program to check prime number
- Java Program to Calculate Simple Interest
- Java Program to Swap Two Numbers Without using a Temporary Variable
- Java Program to Swap Two Numbers
- Java Program to Find ASCII Value of a Character
- Java Program to Check Whether an Alphabet is Vowel or Consonant
- Java Program to Check Leap Year
- Java Program to Multiply Two Numbers
- Java Program to Check Even or Odd Number
- Java Program to Add Two Numbers
- Java Program to Swap Two Strings Without Using Third Variable
- Java Program to Swap Two Strings with Third Variable
- How to Get All Digits from String in Java
- Find Duplicate Number in Array in Java
- How to Get Current Working Directory in Java?
- Check Palindrome String in Java
- Java Program to Create Pyramid Of Numbers
Comments
Post a Comment