Write a C program to find smallest of n numbers.
#include <stdlib.h>
int main() {
int i, n, smallest, *val;
/* get the no of inputs from the user */
printf("Enter the no of inputs:");
scanf("%d", &n);
val = (int *)malloc(sizeof(int) * n);
/* get n numbers from user */
printf("Enter your inputs:\n");
for (i = 0; i < n; i++) {
printf("val[%d] : ", i);
scanf("%d", &val[i]);
}
/* find the smallest of n numbers */
smallest = val[0];
for (i = 1; i < n; i++) {
if (val[i] < smallest)
smallest = val[i];
}
/* print the result */
printf("Smallest of %d numbers is %d\n", n, smallest);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the no of inputs:7
Enter your inputs:
val[0] : 890
val[1] : 100
val[2] : 250
val[3] : 110
val[4] : 220
val[5] : 180
val[6] : 240
Smallest of 7 numbers is 100
Enter the no of inputs:7
Enter your inputs:
val[0] : 890
val[1] : 100
val[2] : 250
val[3] : 110
val[4] : 220
val[5] : 180
val[6] : 240
Smallest of 7 numbers is 100
No comments:
Post a Comment