w3resource

Java: Accepts three numbers and prints


Compare Three Numbers (Equal, Different)

Write a Java program that accepts three numbers and prints "All numbers are equal" if all three numbers are equal, "All numbers are different" if all three numbers are different and "Neither all are equal or different" otherwise.

Test Data
Input first number: 2564
Input second number: 3526
Input third number: 2456

Pictorial Presentation:

Java conditional statement Exercises: Accepts three numbers and prints


Sample Solution:

Java Code:

import java.util.Scanner;
public class Exercise30 {

  public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Input first number: ");
        int x = in.nextInt();
        System.out.print("Input second number: ");
        int y = in.nextInt();
        System.out.print("Input third number: ");
        int z = in.nextInt();

        if (x == y && x == z)
        {
            System.out.println("All numbers are equal");
        }
        else if ((x == y) || (x == z) || (z == y))
        {
            System.out.println("Neither all are equal or different");
        }
        else
        {
            System.out.println("All numbers are different");
        }
    }
}

Sample Output:

Input first number: 2564                                                                                      
Input second number: 3526                                                                                     
Input third number: 2456                                                                                      
All numbers are different

Flowchart:

Flowchart: Java Conditional Statement Exercises - Accepts three numbers and prints


For more Practice: Solve these Related Problems:

  • Write a Java program to compare three integers and print a specific message if all are equal, all are distinct, or if only two match.
  • Write a Java program to store three numbers in an array and then determine if the array contains duplicate values.
  • Write a Java program to compare three numbers using nested ternary operators and output the appropriate message.
  • Write a Java program to sort three numbers and then check if the smallest and largest are equal to the middle value.

Go to:


PREV : Count Digits in an Integer.
NEXT : Order of Three Numbers (Increasing/Decreasing).

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.