Hey Mate! Solve the program in JAVA.
Write a program to display the greatest of three unequal numbers using Input stream reader.
Answers
Greatest of Three Numbers - Java
We are supposed to take user input with Input Stream Reader. We import the java.io package to implement it.
After creating a BufferedReader object, we take the user input using its String readLine() function.
Then, we use a few if conditional blocks to get the greatest of the three numbers. The program works even if any two or all numbers are equal.
import java.io.*;
public class GreatestNumber
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the first number: ");
int num1 = Integer.parseInt(br.readLine());
System.out.print("Enter the second number: ");
int num2 = Integer.parseInt(br.readLine());
System.out.print("Enter the third number: ");
int num3 = Integer.parseInt(br.readLine());
int greatestNumber = num1; //Initialise with num1
if(num1 < num2 || num1 < num3) //If num1 is smaller than any of the other two numbers, go in
{
if(num1 <= num3 && num2 <= num3) //Compare for num3 as greatest
{
greatestNumber = num3;
}
if(num1 <= num2 && num3 <= num2) //Compare for num2 as greatest
{
greatestNumber = num2;
}
}
System.out.println("The greatest number is: "+greatestNumber); //Print Greatest Number
}
}