- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements