Program to find Greatest of Two 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, 55; Output: 55 is greater than 45

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();
         if(n > m)
         {
             System.out.println(n + " is greater than " + m);
         }
         else
         {
             System.out.println(m + " is greater than " + n);
         }
     }
 }

Code Written in BlueJ

Output:
Enter first number: 
45
Enter second number: 
55
55 is greater than 45

Code Written in BlueJ

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.
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 two 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 Two Numbers (Java) Program to find Greatest of Two Numbers (Java) Reviewed by Get2Know on October 01, 2020 Rating: 5

No comments:

Business

Powered by Blogger.