Find Greatest Number Program in Java

Google Advertisements

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

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects

Google Advertisements


Buy Websites 9999595223

Buy Fresh and Payment Receive Media.net Account with Website. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs