103k views
0 votes
Write a C program to find the smallest of n numbers using a while loop.

User Adesina
by
8.7k points

1 Answer

1 vote

Final answer:

The example code uses a while loop to compare a series of numbers entered by the user, updating the minimum value when a smaller number is found, to finally print out the smallest number.

Step-by-step explanation:

The question relates to writing a C program to find the smallest of n numbers using a while loop. Below is an example code that demonstrates this:

#include int main() { int n, i = 1; float num, min; printf("Enter the number of elements: "); scanf("%d", &n); printf("Enter the first number: "); scanf("%f", &min); while(i < n) { printf("Enter another number: "); scanf("%f", &num); if(num < min) { min = num; } i++; } printf("The smallest number is: %.2f", min); return 0; }

Here, we first take the number of elements from the user and initialize the variable for the smallest number (min) with the first value entered. We then enter a while loop that runs (n-1) times to compare the input numbers and find the smallest one.

To write a C program to find the smallest of n numbers using a while loop, you can start by declaring variables to store the numbers and their minimum value. Then, prompt the user to enter the value of 'n' and the 'n' numbers.

Next, initialize the minimum value variable with the first number entered. After that, use a while loop to compare the remaining 'n-1' numbers with the current minimum value

If any number is smaller, update the minimum value variable. Finally, print the smallest number after the loop ends.

User Juan Ariza
by
8.2k points