In this tutorial, we learn about c program to find maximum and minimum of n numbers, with use array. In which just user entered numbers into array and display result.
Assume, first element as minimum i.e. min = arr[0] and maximum i.e.max = arr[0] and check all entered numbers using for loop that current element is greater than max or current element is less than min and display maximum and minimum at the end of for loop.
Assume, first element as minimum i.e. min = arr[0] and maximum i.e.max = arr[0] and check all entered numbers using for loop that current element is greater than max or current element is less than min and display maximum and minimum at the end of for loop.
Source Code
/*
* C program to find maximum and minimum of N numbers with array
*/
#include<stdio.h>
#define size 5 // Maximum array size
int main()
{
int i,max,min,a[size];
printf("Enter elements in array");
for(i=0;i<=4;i++)
{
printf("\nEnter element number \t",i+1);
scanf("%d",&a[i]);
}
printf("\nEntered 10 arrray elements are:-\t");
for(i=0;i<=4;i++)
{
printf("\n\ta[%d]=%d\n",i,a[i]);
}
/* Assume first element as maximum and minimum */
max=a[0];
min=a[0];
/*
* Find Maximum and Minimum in All Array Elements.
*/
for(i=0;i<=4;i++)
{
if(a[i]>max) // If current element is greater than max
{
max=a[i];
}
else
{
if(a[i]<min) // If current element is smaller than min
{
min=a[i];
}
}
}
printf("\nMaximum = %d",max); /* Print maximum number */
printf("\nMinimum = %d",min); /* Print minimum number */
return 0;
}