Java program to find the largest in three numbers using nested if code example

Example 1: Java program to find the largest in three numbers using nested if

// Java program to find largest of four numbers using nested if
public class LargestNestedIfDemo 
{
   public static void main(String[] args) 
   {
      int num1 = 36, num2 = 35, num3 = 56;
      if(num1 >= num2) 
      {
         if(num1 >= num3)
         {
            System.out.println(num1 + " is largest number.");
         }
         else
         {
            System.out.println(num3 + " is largest number.");
         }
      } 
      else 
      {
         if(num2 >= num3)
         {
            System.out.println(num2 + " is largest number.");
         }
         else
         {
            System.out.println(num3 + " is largest number.");
         }
      }
   }
}

Example 2: Java program to find largest of three numbers

java program find largest of three numbers using command line argument
public class UsingCommandLineArgument
{
   public static void main(String[] args) 
   {
      int number1 = Integer.parseInt(args[0]);
      int number2 = Integer.parseInt(args[1]);
      int number3 = Integer.parseInt(args[2]);   
      if(number1 > number2 && number1 > number3)
      {
         System.out.println(number1 + " is largest number.");
      }
      else if(number2 > number1 && number2 > number3)
      {
         System.out.println(number2 + " is largest number.");
      }
      else   
      {
         System.out.println(number3 + " is largest number.");
      }
   }
}

Example 3: Java program to find largest of three numbers

Here’s the largest of three numbers in java using function
import java.util.Scanner;
public class UsingFunction 
{
   public static void main(String[] args) 
   {
      int number1 = 0, number2 = 0, number3 = 0;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter first number: ");
      number1 = sc.nextInt();
      System.out.print("Please enter second number: ");
      number2 = sc.nextInt();
      System.out.print("Please enter third number: ");
      number3 = sc.nextInt();
      System.out.println("Largest of three numbers using function: " + functionLargest(number1, number2, number3));
      sc.close();
   }
   static int functionLargest(int number1, int number2, int number3)
   {
      if(number1 > number2 && number1 > number3)
      {
         return number1;
      }
      else if(number2 > number1 && number2 > number3)
      {
         return number2;
      }
      else
      {
         return number3;
      }
   }
}

Example 4: Java program to find largest of three numbers

Java program to find largest of four numbers using if-else
import java.util.Scanner;
public class FourNumbersUsingIfElse 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter first number to find largest: ");
      int number1 = sc.nextInt();
      System.out.println("Please enter second number to find largest: ");
      int number2 = sc.nextInt();
      System.out.println("Please enter third number to find largest: ");
      int number3 = sc.nextInt();
      System.out.println("Please enter fourth number to find largest: ");
      int number4 = sc.nextInt();
      if(number1 > number2 && number1 > number3 && number1 > number4)
      {
         System.out.println(number1 + " is largest.");
      }
      else if(number2 > number1 && number2 > number3 && number2 > number4)
      {
         System.out.println(number2 + " is largest.");
      }
      else if(number3 > number1 && number3 > number2 && number3 > number4)
      {
         System.out.println(number3 + " is largest.");
      }
      else
      {
         System.out.println(number4 + " is largest.");
      }
      sc.close();
   }
}

Example 5: Java program to find largest of three numbers

largest of three numbers in java using if else
import java.util.Scanner;
public class LargestOfThreeNumbersInJava
{
   public static void main(String[] args)
   {
      int a, b, c;
      System.out.println("Please enter three numbers: ");
      Scanner sc = new Scanner(System.in);
      a = sc.nextInt();
      b = sc.nextInt();
      c = sc.nextInt();
      if(a >= b && a >= c)
         System.out.println(a + " is the largest number.");
      else if(b >= a && b >= c)
         System.out.println(b + " is the largest number.");
      else
         System.out.println(c + " is the largest number.");
      sc.close();
   }
}

Tags:

Java Example