• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Finding Maximum of Three Numbers in Java

May 5, 2020 by Admin Leave a Comment

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

Filed Under: Java

Previous Post: « How to Convert an Integer to Octal in Java
Next Post: Addition of Two Numbers in Java »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in