C program to find smallest element in array

C program to find smallest element in array:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>

main()
{
   int a[30],i,num,small;

   printf("Enter no of elements :");
   scanf("%d",&num);

   //Reading n elements from array
   printf("Enter %d element: ",num);
   for(i=0;i<num;i++)
      scanf("%d",&a[i]);

   //Assume first element as smallest
   small=a[0];

   for(i=0;i<num;i++)
    {
      if(a[i]<small)
      {
         small=a[i];
      }
    }
   printf("\nSmallest Element : %d", small);
}

OUTPUT:

Enter no of elements :5
Enter 5 element: 4 8 3 6 9

Smallest Element : 3

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.