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 among two or more numbers using functions in C programming.

Input two numbers: 10

20

Output maximum: 20

Output minimum: 10

Till now we already know how to find maximum or minimum between two numbers using if else or by using switch case or by using conditional/ternary operator. Here we will be coding the logic of any of the above methods to find maximum or minimum into functions.

Happy coding ;)

**Example:**Input two numbers: 10

20

Output maximum: 20

Output minimum: 10

### Required knowledge

Basic C programming, Function, Variable argument listTill now we already know how to find maximum or minimum between two numbers using if else or by using switch case or by using conditional/ternary operator. Here we will be coding the logic of any of the above methods to find maximum or minimum into functions.

### Program to find maximum and minimum of two numbers

/* * 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; /* * Reads input in two numbers from user */ printf("Enter any two numbers: "); scanf("%d%d", &num1, &num2); maximum = max(num1, num2); //Calls the maximum function minimum = min(num1, num2); //Calls the minimum function printf("\nMaximum = %d\n", maximum); printf("Minimum = %d", minimum); return 0; } /** * Finds maximum between two numbers. */ int max(int num1, int num2) { return (num1 > num2 ) ? num1 : num2; } /** * Finds 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

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 and minimum between two or more numbers

/* * 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() { /* * Demonstrates 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\n", min(2, 10, 20)); return 0; } /** * Finds 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); //Gets the next elements in list if(max < cur) max = cur; } va_end(valist); //Clean the memory assigned by valist return max; } /** * Finds 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

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 ;)

### You may also like

- Function programming exercises index.
- C program to find cube of any number using functions.
- C program to find diameter, circumference and area of circle using functions.
- C program to check even or odd using functions.
- C program to check prime, strong, armstrong and perfect numbers using functions.
- C program to find all armstrong numbers between 1 to n using functions.
- C program to print all even or odd numbers between 1 to n using functions.
- C program to find sum of all even or odd numbers between 1 to n using functions.
- C program to find sum of digits of any number using recursion.
- C program to find factorial of any number using recursion.
- C program to find GCD of any number using recursion.

< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)