Challenge: Biggest of Three
Completed
Write a function int max3(int a, int b, int c) that returns the biggest of three numbers. In main, read three numbers and print the result of max3.
Input
Three integers.
Output
The biggest one.
Example
Input
3 9 4
Output
9
Editor
C
Hints
Start with biggest = a.
Compare it with b, then with c, and update it when you find a bigger one.
Solution explanation
We keep the biggest value seen so far and replace it whenever a bigger one comes along.
Show solution
#include <stdio.h>
int max3(int a, int b, int c) {
int biggest = a;
if (b > biggest) {
biggest = b;
}
if (c > biggest) {
biggest = c;
}
return biggest;
}
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%d\n", max3(a, b, c));
return 0;
} 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