If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In Java, you can determine whether a given number is even or odd using the modulo operator %
. An even number is divisible by 2 without any remainder, while an odd number will have a remainder of 1 when divided by 2.
Here's a Java program to check whether a given number is even or odd:
javaCopy codeimport java.util.Scanner;
public class EvenOddChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
// Check if the number is even or odd
if (number % 2 == 0) {
System.out.println(number + " is an even number.");
} else {
System.out.println(number + " is an odd number.");
}
scanner.close();
}
}
Example output:
csharpCopy codeEnter a number: 25
25 is an odd number.
In the program above, we take the user input and store it in the variable number
. We then use the if
statement to check if the number is even or odd. If the remainder of number
divided by 2 is equal to 0, it is even, and the program prints the message "is an even number." Otherwise, if the remainder is 1, it is odd, and the program prints the message "is an odd number."
Remember that 0 is considered an even number since it is divisible by 2 without a remainder. Additionally, negative numbers can also be even or odd based on the same rule.
Under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use.
Comments: 0