Challenge: Biggest of Three
Completed
Write a function int maxOfThree(int a, int b, int c) that returns the biggest of three numbers. Read three numbers in main and print the result of your function.
Input
Three integers.
Output
The biggest one.
Example
Input
3 9 4
Output
9
Editor
C++
Hints
Start by assuming a is the biggest.
Compare with b and then with c, updating your answer.
Solution explanation
Keep the best answer so far in a variable and compare it with each of the others.
Show solution
#include <iostream>
using namespace std;
int maxOfThree(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;
cin >> a >> b >> c;
cout << maxOfThree(a, b, c) << "\n";
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