Write a function int max_of_four(int a, int b, int c, int d) which reads four arguments and returns the greatest of them.
Question
Write a function
int max_of_four(int a, int b, int c, int d)
which reads four arguments and returns the greatest of them.
Solution
Sure, here is a simple function in C that takes four integers as arguments and returns the maximum among them.
#include <stdio.h>
int max_of_four(int a, int b, int c, int d) {
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
if (d > max) {
max = d;
}
return max;
}
int main() {
int a = 1, b = 2, c = 3, d = 4;
printf("The maximum number is %d\n", max_of_four(a, b, c, d));
return 0;
}
In this function, we first assume that a
is the maximum. Then we compare max
with b
, c
, and d
respectively. If any of these is greater than max
, we update max
. Finally, we return max
, which will be the greatest of the four numbers.
Similar Questions
Write a C++ code to find largest among 4 numbers using the concept call by reference.
Write a program to display a menu driven facility to find the greatest of two numbers,three numbers and four numbers
What is the value of max in the below code snippet?int val1 = 10;int val2 = 20;int max = val1 >= val2 ? val1 : val2;
Develop a Java program to identify the largest among four numbers (32, 29,34, 21) using appropriate control statement and operators.
If a − 1 = b + 2 = c − 3 = d + 4 = e − 5, then the largest number among a, b, c, d, and e is
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.