18 Aug Java Program to get maximum of three integer values
Declare and initialize three integer variables:
1 2 3 4 5 6 |
// 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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
1 2 3 |
Maximum Value = 5 |
No Comments