In this tutorial you will learn about the Java Program to Find Greatest Number and its application with practical example.
Java Program to Find The Greatest Number
In this tutorial, we will learn to create a Java program that will Find The Greatest Number using Java programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- Conditional statement in Java programming.
Program to Find the Greatest Number
In Java programming, it is possible to take multiple integer inputs from the user and Find the Greatest Number with the help of a very short program. The Java language has many types of header libraries which has supported function in them, with the help of these files the programming is easy. But today we will Find the Greatest Number from the taken numbers from the user using Java programming.
Algorithm:-
With the help of this program, we can Find the Greatest Number.
1 2 3 4 5 6 7 8 9 |
1. Declaring the variables for the program. 2. Taking the input numbers. 3. Checking the greatest number. 4. Printing the greatest number. 5. End the program. |
Program to Find the Greatest Number:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.Scanner; //Importing the Scanner class in program public class Great//Main Class Of the function { //Body of the main function of the program. public static void main(String[] args) { //Declaring the required variables for the program. int x, y, z, greatest, flag; //x = it will hold the first number. //y = it will hold the second number. //z = it will hold the third number //object of the Scanner class Scanner sc = new Scanner(System.in); //reading input from the user //Taking the first input number System.out.println("Enter the first number:"); x = sc.nextInt(); //Taking the second input number System.out.println("Enter the second number:"); y = sc.nextInt(); //Taking the third input number System.out.println("Enter the third number:"); z = sc.nextInt(); //comparing x and y and storing the greatest number in a flag variable flag=x>y?x:y; //comparing the flag variable with z and storing the result in the variable greatest=z>flag?z:flag; //prints the greatest number System.out.println("The greatest number is: "+greatest); } } |
Output:-
In the above program, we have first initialized the required variable.
- x= it will hold the first integer value.
- y = it will hold the second integer value.
- z = it will hold the third integer value.
Input numbers for the program.
Program Logic Code.
Printing output for the greatest number.