Program to find Greatest of Three Numbers (Java)
Java Programs
So today we are going to write program that accepts two numbers from the user and find the greatest number from them. So this is a very simple program and it does not require much logic to write this program.
Q. Write a program to accept two number from the user and print the greatest number from them.
For Example - Input: 45, 35,55; Output: 55 is greater than 45 and 35
Solution:
import java.util.*; class GreatestNumber { public static void main() { Scanner sc = new Scanner(System.in); System.out.println("Enter first number: "); int n = sc.nextInt(); System.out.println("Enter second number: "); int m = sc.nextInt(); System.out.println("Enter third number: "); int o = sc.nextInt(); if(n > m && n > o) { System.out.println(n + " is greater than " + m + " and " + o); } if(m > n && m > o) { System.out.println(m + " is greater than " + n + " and " + o); } else { System.out.println(o + " is greater than " + m + " and " + n); } } }
Output:
Enter first number: 45 Enter second number: 35 Enter third number: 55 55 is greater than 35 and 45
Variable description used in program:
int n - It is used to accept a number from the user.
int m - It is used to accept a number from the user.
int o - It is used to accept a number from the user.
Explanation:
In this program we have used Scanner class to accept number from the user. And then we used if-else condition to find the greatest number among the three numbers. This is how this program works.
So that is it to for Finding Greatest of Two Numbers. Hope you find it useful and if you have any query do comment in the comment box and I will try to solve your query. Will see you on next program.
Thank You.
Program to find Greatest of Three Numbers (Java)
Reviewed by Get2Know
on
October 13, 2020
Rating:
No comments: