Sunday, August 7, 2011

Largest and smallest among n numbers

#include<stdio.h>
void main()
{
int n,i=1,num,big,small;
clrscr();
printf("Among how many numbers : ");
scanf("%d",&n);
if(n<0)
  printf("invalid input");
else
  {
  printf("Enter number %d :",i);
  scanf("%d",&num);
  big=small=num;
  for(;i<n;++i)
     {
     printf("Enter number %d :",i+1);
     scanf("%d",&num);
     if(big<num)
       big=num;
     if(small>num)
       small=num;
     }

  printf("largest number is : %d",big);
  printf("\nSmallest number is : %d",small);
  }
getch();
}

No comments:

Post a Comment