Java Programming | Largest Number

Join Programming and Development Chat Groups on Uphub.io

Introduction

This is a tutorial for largest number in java. The program takes two numbers and decides the largest number and prints it. The program is extendable. Go enjoy the program. Lets begin…..

Program to find the largest number in java.


//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
public class LargestNo {
//void main
public static void main (String[] args)
{
//declare int
int a,b;
//print message
System.out.println("Enter two numbers:");
//Take input
Scanner input = new Scanner(System.in);
a = input.nextInt();
b = input.nextInt();
//if else statement
if(a>b)
System.out.println(a+" is largest");
else
System.out.println(b+" is largest");
}
}

Output

Enter two numbers:
12
54
54 is largest

How it works

  1. The program prints the message to enter two number.
  2. The user enters two numbers.
  3. If-else statement decides the largest number and prints it.

Extending it

The program can be extended by using more numbers and finding the largest out of them. Go extend it.

Explanation.

  1. Import the Scanner.
  2. Declare the class as public
  3. Add the void main function
  4. Add system.out.println() function with the message to enter two numbers.
  5. Declare input as Scanner.
  6. Take the inputs and save it in variables.
  7. Add a if-else statement to find the largest number.
  8. Add system.out.println() function to print the number.

At the end.

You learnt creating the Java program for Largest number. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Sharing options: