C Program to find smallest element of array
Simple C program to find smallest element in array: Here first we define min as 0th element of array. Now we compare it with others element of array. If min is greater than array element than we update it to array element. If min is less than array element then do not update it.
Here INT_MAX is maximum value of integer in limits.h library in C language. First we initialize min as INT_MAX and then we compare it with others. If find an array element less than min then update min as that array's element.
#include<stdio.h> int main() { int arr[6]={6,13,9,2,14,8}; int min=arr[0],i=0; /* here 6 is array size */ for(i=0;i<6;i++) { if(min>arr[i]) min=arr[i]; } printf("Smallest element in array = %d\n",min); return 0; }Another C program to find smallest element of array using while loop
#include<stdio.h> int main() { /* 6 is array size */ int arr[6]={60,130,50,36,140,93}; int min=arr[0],i=0; while(i<6) { if(min>arr[i]) { min=arr[i]; } i++; } printf("Smallest element in array = %d\n",min); return 0; }C program to find smallest element of array using INT_MAX
Here INT_MAX is maximum value of integer in limits.h library in C language. First we initialize min as INT_MAX and then we compare it with others. If find an array element less than min then update min as that array's element.
#include<stdio.h> #include<limits.h> int main() { /* 6 is array size */ int arr[6]={23,58,21,39,94,63}; int min=INT_MAX,i=0; for(i=0;i<6;i++) { if(arr[i]<min) min=arr[i]; } printf("Smallest element in array = %d\n",min); return 0; }