Java Program to get maximum of three integer values

Declare and initialize three integer variables:

// 3 integers
int p = 2;
int q = 5;
int r = 3;

Now, we will check for maximum value from the above three integers. Use the greater than (>) and less than (<) Relational operator for comparison as in the below example.

Example

Following is how to get maximum value from three integer values in Java:

public class Example {

  public static void main(String[] args) {
   
   // 3 integers
   int p = 2;
   int q = 5;
   int r = 3;
   
   // checking for maximum value
   if (q > p) {
     p = q;
   }
   if (r > p) {
     p = r;
   }
   
   // displaying maximum from three integers
   System.out.println("Maximum Value = "+p);  
  }
}

Output

Maximum Value = 5
Java Program for Minimum and Maximum values of datatype int
Java Program to get minimum of three integer values
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment