%
) operator to divide the number by 2.
public class CheckEvenOddNo
{
public static void main(String[] args)
{
int number = 31;
// Check if the number is even or odd using conditional logic
if(number % 2 == 0)
{
System.out.println(number + " is an even number.");
}
else
{
System.out.println(number + " is an odd number.");
}
}
}
31 is an odd number.
import java.util.Scanner;
public class CheckEvenOddNo
{
public static void main(String[] args)
{
// Import Scanner Class to take user input
Scanner scanner = new Scanner(System.in);
// Take input from the user
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Check if the number is even or odd using conditional logic
if(number % 2 == 0)
{
System.out.println(number + " is an even number.");
}
else
{
System.out.println(number + " is an odd number.");
}
// Close the scanner to release resources
scanner.close();
}
}
Enter a number: 26 26 is an even number.
Scanner
class is imported to take user input.
number
) is declared to store the user’s input.
scanner.nextInt()
reads the input and stores it in the number
variable.
if
condition checks if number % 2
is equal to 0. If true, it means the number is divisible by 2 and is an even number.
scanner.close()
method is used to close the input stream and release resources.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.