0

*Find the smallest number*. I tried this code below but it gave me 0. Someone can help me please

#include <stdio.h> int main() { int counter = 0, number = 0, smallest = 0; while ( counter < 10 ) { printf( "Enter a number: \n" ); scanf( "%d", &number ); if ( number <= 0 ){ printf(“%d is invalid input!\n”, number); if ( number <= smallest ) { largest = number; } counter++; } printf( "The smallest number is %d\n", largest ); return 0; }

12th Nov 2019, 12:29 PM
Emmanuel Mayambi
2 Réponses
+ 1
1. Store entered numbers in Array of size 10 2. Print the same to ensure that you stored them properly. Then will check next steps...
12th Nov 2019, 1:02 PM
Kuri
Kuri - avatar
+ 1
That's because you initialized smallest to 0. So any number entered larger than 0, you'll find smallest number 0. Two ways to solve this issue 1) Initialize smallest to very high number like 999999. But that's not the proper way for doing it. So prefer to implement second approach. 2) Inside a loop, add this statement if(counter == 0) smallest = number ; This way the first time user enter any number it will be stored in smallest variable and later on will keep comparing the numbers and will find the minimum number.
12th Nov 2019, 1:16 PM
blACk sh4d0w
blACk sh4d0w - avatar