C program to find maximum of four integers by defining function


Suppose we have four numbers a, b, c, and d. We shall have to find maximum among them by making our own function. So we shall create one max() function that takes two numbers as input and finds the maximum, then using them we shall find maximum of all four numbers.

So, if the input is like a = 5, b = 8, c = 2, d = 3, then the output will be 8

To solve this, we will follow these steps −

  • define a function max(), this will take x and y

  • return maximum of x and y

  • take four numbers a, b, c, and d

  • left_max := max(a, b)

  • right_max := max(c, d)

  • final_max = max(left_max, right_max)

  • return final_max

Example

Let us see the following implementation to get better understanding −

#include <stdio.h>
int max(int x, int y){
    if(x > y){
        return x;
    }else{
        return y;
    }
}
int main(){
    int a = 5, b = 8, c = 2, d = 3;
    int left_max = max(a, b);
    int right_max = max(c, d);
    int final_max = max(left_max, right_max);
    printf("Maximum number is: %d", final_max);
}

Input

a = 5, b = 8, c = 2, d = 3

Output

Maximum number is: 8

Updated on: 14-Sep-2023

31K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements