In: Computer Science
write a java program that takes three numbers from the user and print the greatest number (using if-statement).
sample output:
input the first number:35
input the second number:28
input the third number:87
the greatest number:87
CODE:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Input the first number: ");
int num1 = sc.nextInt();
System.out.print("Input the second number: ");
int num2 = sc.nextInt();
System.out.print("Input the third number: ");
int num3 = sc.nextInt();
// Check if num1 is greater than other 2 numbers
if (num1 > num2)
if (num1 > num3)
System.out.println("The greatest number: " + num1);
// Check if num2 is greater than other 2 numbers
if (num2 > num1)
if (num2 > num3)
System.out.println("The greatest number: " + num2);
// Check if num3 is greater than other 2 numbers
if (num3 > num1)
if (num3 > num2)
System.out.println("The greatest number: " + num3);
}
}
OUTPUT:
Input the first number: 35
Input the second number: 28
Input the third number: 87
The greatest number: 87
SUMMARY:
The above code is written in java as instructed. First we scan the value from user by asking to input 3 numbers. Then using if statements we find the greatest of 3 numbers and print the output accordingly.
Note: If you have any queries please let me know in comments. If not than a Like would be appreciated