Find Greatest Number Program in Java


Prev Tutorial Next Tutorial

Java Program to Find Largest Number Among Three Numbers

Input three number from user and compare these number with each others and find largest number among these three numbers. Here i am using ternary operator for find largest number. Read more about ternary operator Click here

Find Largest Number Program in Java

import java.util.Scanner;

class greatestnumber
{
public static void main(String[] args) 
{
int a,b,c,largest;
Scanner s=new Scanner(System.in);
System.out.println("Enter any three numbers: ");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();

if(a>=b && a>=c) 
{
System.out.println("Largest number: "+a);
}
if(b>=a && b>=c) 
{
System.out.println("Largest number: "+b);
}
if(c>=a && c>=b) 
{
System.out.println("Largest number: "+c);
}
}
}

Output

Enter any three numbers :
7 
24
3
Largest number: 24

Find Largest Number Among Three Numbers using Ternary Operator in Java

import java.util.Scanner;
class greatestnumber
{
public static void main(String[] args) 
{
int a,b,c,largest;
Scanner s=new Scanner(System.in);
System.out.println("Enter any three numbers: ");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
largest=(a>b)?(a>c?a:c):(b>c?b:c);
System.out.println("Largest number: "+largest);
}
}

Output

Enter any three numbers :
7 
6
11
Largest number: 11

Syntax to compile and run java program

Syntax

for compile -> c:/>javac greatestnumber.java
for run -> c:/>java greatestnumber

Explanation of Code

  • Scanner s=new Scanner(System.in): are used for receive input from keyboard.
  • nextInt(): method are used for get integer type value from keyboard.
  • System.out.println("....."): are used for display message on screen or console.

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements