Max of 3 Using Nested If
Find the maximum of three numbers using nested if statements.
BeginnerTopic: Module 2: Conditional Programs
Java Max of 3 Using Nested If Program
This program helps you to learn the fundamental structure and syntax of Java programming.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
System.out.print("Enter third number: ");
int c = sc.nextInt();
int max;
if (a >= b) {
if (a >= c) {
max = a;
} else {
max = c;
}
} else {
if (b >= c) {
max = b;
} else {
max = c;
}
}
System.out.println("Maximum = " + max);
sc.close();
}
}Output
Enter first number: 3 Enter second number: 9 Enter third number: 7 Maximum = 9
Understanding Max of 3 Using Nested If
We use nested if inside another if to compare three numbers step by step.
Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.