Difference between max and min of Array by R4R Team

Example-
Array is [2,1,0,9,4,5,3]
Difference of max and min is 9-0=9

program-

#include< stdio.h>
int main()
{
int a[20],n,i,max,min;
printf("Enter number of elements in arary\n");
scanf("%d",&n);
printf("Enter %d number in array",n);
for(i=0;i< n;i++)
scanf("%d",&a[i]);
//find maximum
max=a[0];
for(i=0;i< n;i++){
if(a[i]>max)
{
max=a[i];
}
}
//find minimum
min=a[0];
for(i=0;i< n;i++){
if(a[i]< min)
{
min=a[i];
}
}
printf("Difference between max and min value is %d",max-min);
}


output-

Enter number of elements in array
8
Enter 8 number in array
3 2 1 4 5 6 7 8
Difference between max and min value is 7

-In this program, Firstly we take an input in array
-Then we run two loops , one for finding the max element and another for finding the min element.
-When we have both max and min, then we subtract min value by max value and result will display on console.




Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!