Java Programs

Find the largest of three numbers

We will learn how to write a program to find the largest of three numbers.

How will this Java program behave?

Suppose someone gives 3 numbers as input 12, 15 and 10, then our program should return 15 as output because 15 is the largest number among three.

Program:

import java.util.*;  

class Main {

    public static void main(String ...args){

        Scanner sc= new Scanner(System.in);
        System.out.print("Enter first number- ");  
        int a= sc.nextInt();  
        System.out.print("Enter second number- ");  
        int b= sc.nextInt();  
        System.out.print("Enter third number- ");  
        int c= sc.nextInt();  
        if(a>=b && a>=c)
    		System.out.println(" a is greatest");
    	if(b>=a && b>=c)
    		System.out.println(" b is greatest");
    	if(c>=a && c>=b)
    		System.out.println("c is gretest");
    }

} 

Output:

Enter first number - 5
Enter second number - 3
Enter third number - 78
c is gretest

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs