Write a program to find the range of the elements in the array.
The range of an array is the difference between the maximum and minimum element in an array,
Input Format:
Input consists of n+1 integers where n corresponds to the number of elements in the array.
The first integer corresponds to n and the next n integers correspond to the elements in the array. Assume that the maximum number of elements in the array is 20.
Output Format:
The output consists of an integer which corresponds to the range of the array.
Refer sample input and output for formatting specifications.
[All text in bold corresponds to the input and the rest corresponds to output.]

Sample Input and Output :
Enter the number of elements in the array
5
Enter the elements in the array
2
4
1
3
5
The range of the array is 4


Program Code:
#include<stdio.h>
int main()
{
 int arr[20],n,i,j,min,max;
 printf("Enter the number of elements in the array");
 scanf("%d",&n);
 printf("\nEnter the elements in the array");
 for(i=0;i<n;i++)
 scanf("%d",&arr[i]);
 min =arr[0];
 for(i=1;i<n;i++)
 {
     if(arr[i]<min)
     min=arr[i];
 }
 max=arr[0];
 for(i=1;i<n;i++)
 {
     if(arr[i]>max)
     max=arr[i];     
 }
j=max-min;
printf("\nThe range of the array is %d",j);
return 0;
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: