java program to find largest of n natural numbers code example

Example 1: 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 2: 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