C program to find largest and smallest number in an array





C code to find largest and smallest number in an array


#include<stdio.h>
int main(){
  int a[50],size,i,big,small;

  printf("\nEnter the size of the array: ");
  scanf("%d",&size);
  printf("\nEnter %d elements in to the array: ", size);
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);

  big=a[0];
  for(i=1;i<size;i++){
      if(big<a[i])
           big=a[i];
  }
  printf("Largest element: %d",big);
 
  small=a[0];
  for(i=1;i<size;i++){
      if(small>a[i])
           small=a[i];
  }
  printf("Smallest element: %d",small);

  return 0;
}


Sample Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1



Alogrithm:
**




6 comments:

  1. what does [50] & [i] mean ? why are they in brackets ?

    ReplyDelete
    Replies
    1. [50] is the size of the array.it means if we have an integer array it aqquire 100 bytes in case of [50].because an integer value took 2 bytes.and "i" is nothing ,it is just written to show the compiler that this is used in the main program....

      Delete
  2. to declare in arrays we have to use brackets.

    ReplyDelete
  3. Y do v need to ask d user for d Size. Directly entering d elements wont work?

    ReplyDelete
  4. they are array and 50 represent array size and i for using loop which represent element position num....

    ReplyDelete
  5. Simple and logical. Thanks a lot :-)

    ReplyDelete