Finding Maximum of Three Numbers in Java

In this post, we will see 2 variations of finding maximum of 3 numbers in Java: (A) Max value of 3, (B) Max variable of 3 numbers.

Max Value of 3

package com.techstackjournal;

public class MaxOfThreeValues {

	public static void main(String[] args) {

		int a = 10, b = 30, c = 30;

		System.out.println("max value is " + findMaxValue(a, b, c));

	}

	private static int findMaxValue(int a, int b, int c) {
		int max;
		if (a >= b && a >= c) {
			max = a;
		} else if (b >= a && b >= c) {
			max = b;
		} else {
			max = c;
		}
		return max;
	}

}
max value is 30

Max Variable of 3

package com.techstackjournal;

public class MaxOfThree {

	public static void main(String[] args) {

		int a = 10, b = 30, c = 30;

		if (a > b) {
			if (a > c) {
				System.out.println("a is max");
			} else if (a == c) {
				System.out.println("a and c are max");
			} else {
				System.out.println("c is max");
			}
		} else if (a == b) {
			if (a > c) {
				System.out.println("a and b are max");
			} else if (a == c) {
				System.out.println("all are equal");
			} else {
				System.out.println("c is max");
			}
		} else if (a < b) {
			if (b > c) {
				System.out.println("b is max");
			} else if (b == c) {
				System.out.println("b and c are max");
			} else {
				System.out.println("c is max");
			}
		}

	}

}
b and c are max
See also  Constructor Calling in Java