# C Program to find maximum and minimum number in an array

Category: C Program

Learn how to write a C program to find the maximum and minimum numbers in an array using functions. This article provides a comprehensive explanation and code example, showcasing the benefits of using functions in C programming.

In programming, finding the maximum and minimum values in an array is a fundamental operation that is often required in various applications, such as data analysis, optimization problems, and statistics. In this article, we will explore how to write a C program that finds the maximum and minimum numbers in an array using separate functions for each operation. We will discuss the importance of using functions, provide a detailed explanation of the program's structure, and offer a complete implementation.

## Understanding the Problem

Given an array of numbers, the goal is to find:

1. The maximum value in the array.
2. The minimum value in the array.

## Write a C Program to find maximum and minimum number in an array

We will implement a C program that includes two functions: `findMax` to find the maximum value and `findMin` to find the minimum value in an array. These functions will take the array and its size as arguments and return the corresponding maximum and minimum values.

``````#include <stdio.h>

// Function to find the maximum number in an array
int findMax(int arr[], int size) {
int max = arr[0]; // Initialize max with the first element
for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i]; // Update max if the current element is greater
}
}
return max;
}

// Function to find the minimum number in an array
int findMin(int arr[], int size) {
int min = arr[0]; // Initialize min with the first element
for (int i = 1; i < size; i++) {
if (arr[i] < min) {
min = arr[i]; // Update min if the current element is smaller
}
}
return min;
}

int main() {
int n;

// Input the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n];

// Input the elements of the array
printf("Enter the elements of the array -\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Call the functions to find max and min
int max = findMax(arr, n);
int min = findMin(arr, n);

// Output the results
printf("The maximum number in the array is: %d\n", max);
printf("The minimum number in the array is: %d\n", min);

return 0;
}
``````

Output

``````Enter the number of elements in the array: 5
Enter the elements of the array -
1
4
7
3
2
The maximum number in the array is: 7
The minimum number in the array is: 1
``````

## Explanation of the Code

1. Function `findMax`:
• This function takes an array `arr[]` and its size `size` as parameters.
• It initializes the `max` variable with the first element of the array.
• The function iterates through the array, updating `max` whenever it finds an element greater than the current `max`.
• It returns the maximum value found in the array.
2. Function `findMin`:
• Similar to `findMax`, this function takes an array `arr[]` and its size `size` as parameters.
• It initializes the `min` variable with the first element of the array.
• The function iterates through the array, updating `min` whenever it finds an element smaller than the current `min`.
• It returns the minimum value found in the array.
3. Main Function `main`:
• The user is prompted to enter the number of elements in the array.
• The elements of the array are then inputted by the user.
• The functions `findMax` and `findMin` are called to determine the maximum and minimum values, respectively.
• The results are displayed to the user.