Search



Thursday, 26 February 2015

smallest element of array

In this program, the task is to take input of n no. of elements in , store it in array & then find the smallest no. from the elements of array. Here , n is the no. of elements in array & Using for loop , we will get the values in order of a[0] , a[1] , - - - - till a[n] . Afterthat , we assume the smallest element is a[0] & then we will check if any no. is smaller than the smallest element we have will be stored in small variable using for loop . hence , we will find smallest element of array.

#include<stdio.h>
#include<conio.h>
void main()
{ int a[20],i=0,n,small;
clrscr();
printf("\n program to find smallest element in array");
printf("\n\n enter no. of elements in array (max 20) : ");
scanf("%d",&n);
printf("\n now enter them : \n");
for(i=0;i<n;i++)
{ printf(" enter a[%d] = ",i);
scanf("%d",&a[i]);
}
printf("\n\n finding smallest element now.. \n");
small = a[0];
for(i=0;i<n;i++)
{ if(a[i]<=small)
small = a[i];
}
printf("smallest element = %d",small);
getch();
}

No comments:

Post a Comment

Guys if you think something is wrong or should be edit than please do comment.