C++ program to find greatest among four input integers


Suppose we have four integers a, b, c and d. We shall have to find the greatest number 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 = 75, b = 18, c = 25, d = 98, then the output will be 98.

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 <iostream>
using namespace std;
int max(int x, int y){
    if(x > y){
        return x;
    }else{
        return y;
    }
}
int main(){
    int a = 75, b = 18, c = 25, d = 98;
    int left_max = max(a, b);
    int right_max = max(c, d);
    int final_max = max(left_max, right_max);
    cout << final_max;
}

Input

75, 18, 25, 98

Output

98

Updated on: 07-Oct-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements