Challenge: Biggest of Three
Completed
Write a method static int maxOfThree(int a, int b, int c) that returns the biggest of three numbers. In main, read three numbers and print the result of your method.
Input
Three whole numbers.
Output
The biggest one.
Example
Input
3 9 4
Output
9
Editor
Java
Hints
Start with max = a, then compare with b and c.
Math.max(a, Math.max(b, c)) also works.
Solution explanation
The method keeps the biggest value seen so far and returns it.
Show solution
import java.util.Scanner;
public class Main {
static int maxOfThree(int a, int b, int c) {
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
System.out.println(maxOfThree(a, b, c));
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module