0
How to fix this , I want the Result to be lowest to highest but the output is the opposite? And there's a error.
#include <stdio.h> int main() { int num, counter; counter=0; printf("Input a number between 0 to 50:"); scanf("%d",&num); printf("\n"); if(num <= 50 && num >= 0) while(0 <= num) { printf("%d %d %d %d %d %d %d %d %d %d \n",num,num - 1,num - 2,num - 3,num - 4,num - 5,num - 6,num - 7,num - 8,num - 9); num -= 10; } else printf("\nInvalid Input"); return 0; } https://code.sololearn.com/cR7H37TIVbS4/?ref=app
2 odpowiedzi
+ 3
This is the output when I inputted 20.
Input a number between 0 to 50: 20
20 19 18 17 16 15 14 13 12 11
10 9 8 7 6 5 4 3 2 1
0 -1 -2 -3 -4 -5 -6 -7 -8 -9
If you want the output to be like this, the code is below.
Input a number between 0 to 50:
0 -1 -2 -3 -4 -5 -6 -7 -8 -9
10 9 8 7 6 5 4 3 2 1
20 19 18 17 16 15 14 13 12 11
Replace your if-statement with this:
if(num <= 50 && num >= 0) {
counter = num;
num = 0;
while(num <= counter) {
printf("%d %d %d %d %d %d %d %d %d %d \n",num,num - 1,num - 2,num - 3,num - 4,num - 5,num - 6,num - 7,num - 8,num - 9);
num += 10;
}
} else
printf("\nInvalid Input");
Using the above code will also fix your warning about counter being unused because the code above actually uses counter. It isn't the best name for a variable used to just remember the original num value but it works.
If you just want to go from 0 to the inputted number with 10 values per line, I would add another loop through the 10 integers in each line instead of having so much similar code repeated for each of the 10.
0
There is a warning because you didn't used the "counter" variable.