Write a C program to input two or more numbers from user and find maximum and minimum of the given numbers using functions. How to find maximum and minimum of two or more numbers using functions in C programming.

**Example**

**Input**

Input two numbers: 10 20

**Output**

Maximum = 20 Minimum = 10

## Required knowledge

Basic C programming, Functions, Returning value from function, Variable length arguments

Must know - Program to find maximum using conditional operator.

## Declare function to find maximum

We already learned to find maximum using conditional operator and using many other approaches. Here, I will embed the logic to find maximum within a function. Let us define function to find maximum.

- First give a meaningful name to our function. Say
`max()`

function is used to find maximum between two numbers. - Second, we need to find maximum between two numbers. Hence, the function must accept two parameters of
`int`

type say,`max(int num1, int num2)`

. - Finally, the function should return maximum among given two numbers. Hence, the return type of the function must be same as parameters type i.e.
`int`

in our case.

After combining the above three points, function declaration to find maximum is `int max(int num1, int num2);`

.

## Program to find maximum and minimum between two numbers using functions

```
/*
* C program to find maximum and minimum between two numbers using functions
*/
#include <stdio.h>
/* Function declarations */
int max(int num1, int num2);
int min(int num1, int num2);
int main()
{
int num1, num2, maximum, minimum;
/* Input two numbers from user */
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);
maximum = max(num1, num2); // Call maximum function
minimum = min(num1, num2); // Call minimum function
printf("\nMaximum = %d\n", maximum);
printf("Minimum = %d", minimum);
return 0;
}
/**
* Find maximum between two numbers.
*/
int max(int num1, int num2)
{
return (num1 > num2 ) ? num1 : num2;
}
/**
* Find minimum between two numbers.
*/
int min(int num1, int num2)
{
return (num1 > num2 ) ? num2 : num1;
}
```

Output

Enter any two numbers: 10 20 Maximum = 20 Minimum = 10

**Note:** You can also use variable argument list to find maximum or minimum between two or more variables at once.

## Program to find maximum using var-args

```
/*
* C program to find maximum and minimum between two numbers or more numbers using functions
*/
#include <stdio.h>
#include <limits.h>
#include <stdarg.h>
/* Function declarations */
int max(int args, ...);
int min(int args, ...);
int main()
{
/*
* Demonstrate the use of variable argument list
*/
printf("Maximum of three numbers: (10, 30, 20) = %d\n", max(3, 10, 30, 20));
printf("Maximum of five numbers: (5, -45, 4, 60, 100) = %d\n", max(5, -45, 4, 60, 100));
printf("Minimum of four numbers: (-5, 0, 10, 50) = %d\n", min(4, -5, 0, 10, 50));
printf("Minimum of two numbers: (10, 20) = %d", min(2, 10, 20));
return 0;
}
/**
* Find maximum between two or more integer variables
* @param args Total number of integers
* @param ... List of integer variables to find maximum
* @return Maximum among all integers passed
*/
int max(int args, ...)
{
int i, max, cur;
va_list valist;
va_start(valist, args);
max = INT_MIN;
for(i=0; i<args; i++)
{
cur = va_arg(valist, int); // Get next elements in the list
if(max < cur)
max = cur;
}
va_end(valist); // Clean memory assigned by valist
return max;
}
/**
* Find minimum between two or more integer variables
* @param args Total number of integers
* @param ... List of integer variables to find minimum
* @return Minimum among all integers passed
*/
int min(int args, ...)
{
int i, min, cur;
va_list valist;
va_start(valist, args);
min = INT_MAX;
for(i=0; i<args; i++)
{
cur = va_arg(valist, int);
if(min > cur)
min = cur;
}
va_end(valist);
return min;
}
```

Output

Maximum of three numbers: (10, 30, 20) = 30 Maximum of five numbers: (5, -45, 4, 60, 100) = 100 Minimum of four numbers: (-5, 0, 10, 50) = -5 Minimum of two numbers: (10, 20) = 10

Happy coding ðŸ˜‰

doubt, write here. I willhelp my best.<pre><code> ----Your Source Code---- </code></pre>