Pages

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

03 January 2012

program to find maximum and minimum of N numbers in array

This first program is to find maximum and minimum of 5 numbers's


#include<conio.h>
#include<stdio.h>
void main()
{
       int i,min,max,arr[5];
       clrscr();
       printf("enter 5 num's:");
       for(i=0;i<5;i++)
       {
             scanf("%d",&arr[i]);
       }
       min=arr[0];
       max=arr[0];
      for(i=1;i<5;i++)
       {
              if(arr[i]<min)
                      min=arr[i];
              if(arr[i]>max)
                      max=arr[i];
       }
        printf("\n%d is max\n%d is minimum",max,min);
}

Explantion:
           Here in this program we are just checking 1 by 1 which is the maximum and                                         minimum number by running the loop for 5 times and placing the smallest number which has been scanned in min in each iteration,comparing elements with the elements  with min.

The below program is to find minimum and maximum of N number's. (User friendly)

#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];
  if(arr[i]>max)
   max=arr[i];
 }
 printf("\n%d is max\n%d is minimum",max,min);
 getch();
}




Explantion:
           Here in this program we are just checking 1 by 1 which is the maximum and                                         minimum number by running the loop for N times and placing the smallest number which has been scanned in min in each iteration,comparing elements with the elements  with min.


No comments:

Post a Comment

Back To Top