C Program To Find Smallest Of N Numbers Using While Loop
In this tutorial, we are going to see how to write a C program to find the smallest of N numbers using while loop. In the following example, we keep asking the user to enter a number(s) bigger than 0. If the user types 0, the program exit. At first, we consider the min as the first input. Then in the loop, we compare it with each input entered by the user.
C Program To Find Smallest Of N Numbers Using While Loop
#include <stdio.h> int main(void) { int n; printf("Enter a number (0 to exit): "); scanf("%d", &n); int min = n; while (n != 0) { if (min > n) { min = n; } printf("Enter a number (0 to exit): "); scanf("%d", &n); } printf("Min is: %d", min); }
Output:
Enter a number (0 to exit): 75 Enter a number (0 to exit): 42 Enter a number (0 to exit): 65 Enter a number (0 to exit): 12 Enter a number (0 to exit): 47 Enter a number (0 to exit): 0 Min is: 12