2/22/2009

C Program To Find The Largest And Smallest Numbers In A Given Set Of Numbers


/* C Program To Find The Largest And Smallest Numbers In A Given Set Of Numbers */
#include<stdio.h>
#include<conio.h>
#include<math.h>


int main()
{
   int n,i,large,small,a[20];
     printf("Enter a number of elements:\n");
         scanf("%d", &n);
         if(n>0)
         {
           printf("Enter the elements one by one:\n");
            for(i=0;i<n;i++)
            {
              scanf("%d", &a[i]);
            }
            i=0;
            large=a[i];
            small=a[i];
            for(i=0;i<n;i++)
            {
             if(a[i]>large)
             {
                 large=a[i];
             }
             else if(a[i]<small)
             {
                 small=a[i];
             }
         }
          printf("Largest number: %d\n",large);
          printf("Smallest number: %d\n",small);
         }

   return 0;
}
Out Put:
Enter a number of elements:
8
Enter the elements one by one:
8
65
42
98
25
1
34
3
Largest number: 98
Smallest number: 1

No comments:

Post a Comment