Java Program to print maximum among three numbers

In this tutorial we will learn writing Java Program to print maximum number among three given number.

For example:

suppose we have given three numbers num1 = 23, num2 = 12 and num3=43 then output should be 43 because 43 is greatest among three.

There are multiple logics and algorithms to write a program to print the maximum among three.

Here we will learn three types of program

  • Find largest/maximum using if…else
  • Find largest/maximum using ternary operator
  • Find largest/maximum using user-defined function

Program 1 : Find largest among three using if…else

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        int num1, num2, num3;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the first number:");
        num1 = s.nextInt();
        System.out.print("Enter the second number:");
        num2 = s.nextInt();
        System.out.print("Enter the third number:");
        num3 = s.nextInt();
        if(num1 > num2 && num1 > num3)
        {
            System.out.println("Maximum number is:"+num1);
        }
        else if(num2 > num3)
        {
            System.out.println("Maximum number is:"+num2);
        }
        else
        {
            System.out.println("Maximum number is:"+num3);
        }
    }
}

Output

Enter the first number:11
Enter the second number:22
Enter the third number:33
Largest number is:33

Program 2 : Find maximum among three using ternary operator

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) 
    {
        int num1, num2, num3;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the first number:");
        num1 = s.nextInt();
        System.out.print("Enter the second number:");
        num2 = s.nextInt();
        System.out.print("Enter the third number:");
        num3 = s.nextInt();
        int largest = num3 > (num1 > num2 ? num1 : num2) ? num3 : ((num1 > num2) ? num1 : num2); 
        System.out.println("Maximum number is:"+largest);
    }
}

Output

Enter the first number:32
Enter the second number:11
Enter the third number:45
Maximum number is:45

Program 3 : Find maximum among three using functions

import java.util.Scanner;
public class Main
{
    public static int LargestAmongThree(int num1, int num2, int num3)
    {
      return num3 > (num1 > num2 ? num1 : num2) ? num3 : ((num1 > num2) ? num1 : num2);
    }
    public static void main(String[] args) 
    {
        int num1, num2, num3;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the first number:");
        num1 = s.nextInt();
        System.out.print("Enter the second number:");
        num2 = s.nextInt();
        System.out.print("Enter the third number:");
        num3 = s.nextInt();
        System.out.println("Maximum number is:"+LargestAmongThree(num1, num2, num3));
    }
}

Output

Enter the first number:34
Enter the second number:11
Enter the third number:21
Maximum number is:34
Scroll to Top