/* Finding Minimum and second minimum numbers in an array. */
#include <stdio.h>
int main()
{
int n, min, sec_min;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n], i;
for(i = 0; i < n; i++){
printf("Enter element %d: ", i+1);
scanf("%d", &a[i]);
}
printf("Array you entered is .....\n");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
min = a[0];
sec_min = a[1];
for(i = 0; i < n; i++){
if(min >= a[i]){
sec_min = min;
min = a[i];
}
if(sec_min >= a[i] && a[i] != min)
sec_min = a[i];
}
printf("\nMinimum number is %d\nSecond Minimum number is %d\n", min, sec_min);
return 0;
}
Comments
Post a Comment