highest of three numbers in java code example

Example 1: how to do the maximum of three numbers in java

public class Example {
   public static void main(String args[]) {
      int num1 = 15;
      int num2 = -5;
      int num3 = 7;
      if (num1 >= num2 && num1 >= num3)
         System.out.println( num1 + " is the maximum number.");
      else if (num2 >= num1 && num2 >= num3)
         System.out.println( num2 + " is the maximum number.");
      else
         System.out.println( num3 + " is the maximum number.");
   }
}

Example 2: 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;
      }
   }
}

Tags:

Java Example