Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Display the Maximum of Three Integer Values in Java



The following is an example displaying the maximum of three integer values.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      int val1 = 10;
      int val2 = 20;
      int val3 = 30;
      System.out.println("Number 1 = "+val1);
      System.out.println("Number 2 = "+val2);
      System.out.println("Number 3 = "+val3);
      if (val2 > val1) {
         val1 = val2;
      }
      if (val3 > val1) {
         val1 = val3;
      }
      System.out.println("The greatest of three numbers: "+val1);
   }
}

Output

Number 1 = 10
Number 2 = 20
Number 3 = 30
The greatest of three numbers: 30

In the above program, we have taken three integer variables, which will be compared.

int val1 = 10;
int val2 = 20;
int val3 = 30;

Now condition is used to check which of the integer value is the greatest.

if (val2 > val1) {
   val1 = val2;
}
if (val3 > val1) {
   val1 = val3;
}

The above returns the greatest value.

Updated on: 2020-06-26T10:08:31+05:30

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements