Java Program to Find the Greatest of the Three Numbers

Find the Greatest of Three Numbers in Java

Given three integers num1, num2, and num3 as inputs.

The objective is to write a code to Find the Greatest of the Three Numbers in Java Language. To do so we’ll check the numbers with each other and print the largest of them all.

Example

Input : num1 = 2 num2 = 9 num3 = 4

Output : 9

Flow Diagram:-

Greatest of the Three numbers.jpg

Code:-

  • Declare a variable a, b, c and largest as integer;

  • Read the number a, b and c;

  • max = a > b ? (a > c ? a : c) : (b > c ? b : c);

  • print max;

Algorithm:-

Code:-

public class Main
{
  public static void main (String[]args)
  {

    int num1 = 10, num2 = 20, num3 = 30;

    //checking if num1 is greatest
    if (num1 >= num2 && num1 >= num3)
        System.out.println (num1 + " is the greatest");

    //checking if num2 is greatest
    else if (num2 >= num1 && num2 >= num3)
        System.out.println (num2 + " is the greatest");

    //checking if num2 is greatest
    else if (num3 >= num1 && num3 >= num2)
        System.out.println (num3 + " is the greatest");
  }
}

Output:-

Enter a number: 10 15 20

20 is the greatest