Pages

  • Home
  • About & Contact
  • Archive
  • C Programming Examples

02 January 2012

maximum and minimum numbers in array in c

Here is the program to find minimum and maximum number in an array  
find minmum and maximum value in an array

#include<conio.h>
#include<stdio.h>
void main()
{
int i,min,max,num,arr[5];
clrscr();
printf("enter num of elements to be entered:");
scanf("%d",&num);
printf("Enter %d num's",num);
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
min=arr[0];
max=arr[0];
for(i=1;i<num;i++)
{
if(arr[i]<min)
min=arr[i];                            // storing the index of minimum value in min
if(arr[i]>max)                                  
max=arr[i];                           // storing the index of maximum value in max
}
printf("\n%d is max\n%d is minimum",max,min);
getch();
}

Explanation:  In this program we are saving the index of minimum and maximum number in variable's min and max respectively,then comparing elements at this index with rest of the array value's linearly.

1 comment:

  1. joyson joy garuda05 June, 2013

    sexy....thankyou

    ReplyDelete