Write a Java program that accepts four integers from the user and prints equal if all four are equal, and not equal otherwise.
CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Accepting four integers from the user
System.out.println("Enter the first integer:");
int num1 = scanner.nextInt();
System.out.println("Enter the second integer:");
int num2 = scanner.nextInt();
System.out.println("Enter the third integer:");
int num3 = scanner.nextInt();
System.out.println("Enter the fourth integer:");
int num4 = scanner.nextInt();
// Checking if all four integers are equal
if (num1 == num2 && num2 == num3 && num3 == num4) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}
scanner.close();
}
}
- Import the Scanner class to read input from the user.
- Define a class named Main.
- Define the main method, which is the entry point of the program.
- Create a Scanner object named scanner to read input from the user.
- Prompt the user to enter the first integer.
- Read the first integer entered by the user and store it in the variable num1.
- Prompt the user to enter the second integer.
- Read the second integer entered by the user and store it in the variable num2.
- Prompt the user to enter the third integer.
- Read the third integer entered by the user and store it in the variable num3.
- Prompt the user to enter the fourth integer.
- Read the fourth integer entered by the user and store it in the variable num4.
- Check if all four integers (num1, num2, num3, num4) are equal using the logical AND (&&) operator.
- If all four integers are equal, print "Equal".
- If not all four integers are equal, print "Not Equal".
- Close the Scanner object to release system resources.
- End of the program.