Exercise: Maximum of 3 numbers
Let's wrap up what we have learned at this point:
- variables: places to store values in memory, each variable has a type, and we have only used
inttype for now; - assignment: an operator
=which assigns a value to a variable; - functions: a piece of code that takes its parameters and calculates a return value based on those parameters;
- comparison operators:
>,<,>=,<=,==,!=; printffunction which prints the given"string", replacing format specifiers like%dwith the values of its parameters;ifconditional statement.
Let's use what you learned to write a function which returns the greater of 3 numbers. There are many different ways how to solve it, just pick one!
#include <stdio.h>
int max3(int a, int b, int c) {
/* your code goes here! */
return 0;
}
int main() {
printf("max3(1, 2, 3) is %d\n", max3(1, 2, 3));
printf("max3(4, 1, 2) is %d\n", max3(4, 1, 2));
printf("max3(1, 5, 2) is %d\n", max3(1, 5, 2));
printf("max3(4, 4, 3) is %d\n", max3(4, 4, 3));
printf("max3(1, 2, 2) is %d\n", max3(1, 2, 2));
printf("max3(1, 0, 1) is %d\n", max3(1, 0, 1));
return 0;
}
Make sure your code passes the test!