Decrementing Numbers from 10 to 1 Using Loops in C
For Loop
The for loop is commonly used when the number of iterations is known beforehand. In C, we can use a for loop to decrement a number from 10 to 1 as follows:
![](https://www.simplilearn.com/ice9/free_resources_article_thumb/c-for-loop.JPG)
#include <stdio.h>
int main() {
int i;
for(i=10; i>=1; i--) {
printf("%d\n", i);
}
return 0;
}
In the for loop declaration, we initialize the variable i to 10. The condition i>=1 checks if i is greater than or equal to 1 before each iteration. i– decrements i by 1 after each iteration. Within the loop body, we simply print the value of i. This will output the numbers from 10 to 1. The for loop structure makes it very clear that we want to decrement a counter variable from an initial value to a final value in fixed increments. All the loop initialization, condition, and increment/decrement operations are defined neatly in one place.
While Loop
We can also use a while loop to decrement a number from 10 to 1. The code would be:
#include <stdio.h>
int main() {
int i = 10;
while(i >= 1) {
printf("%d\n", i);
i--;
}
return 0;
}
Here we initialize i to 10 outside the loop. The condition i>=1 is checked at the start of each iteration. We print i and decrement it by 1 using i– inside the loop body. The while loop works well when we don’t know the exact number of iterations beforehand. It keeps executing the loop body as long as the given condition is true.
Do While Loop
Another option is to use a do while loop:
#include <stdio.h>
int main() {
int i = 10;
do {
printf("%d\n", i);
i--;
} while(i >= 1);
return 0;
}
The do while loop is similar to a regular while loop, but it executes the loop body at least once even if the condition is false, before checking the condition. Here i is initialized to 10 outside the loop. The loop body prints i and decrements it by 1. The condition i>=1 is checked at the end of each iteration to determine whether to repeat the loop. The do while loop structure ensures the loop body is executed at least once, which is useful when we want to perform an operation at least once regardless of the condition.
Choosing the Right Loop
All three loops - for, while and do while - can be used to decrement a number from 10 to 1. However, the for loop is generally preferred for this purpose due to its clean and concise structure. The initialization, condition check and increment/decrement are defined together in the for loop header, making the flow and purpose very clear. In contrast, the while and do while loops require initializing and updating the counter variable separately. For fixed iterations with a counter, the for loop structure matches the problem very well. Overall, the for loop provides the cleanest and most readable way to decrement a number from 10 to 1 in C.
Printing on the Same Line
So far we have printed each number on a new line. We can also print all the numbers on the same line separated by spaces:
#include <stdio.h>
int main() {
for(int i=10; i>=1; i--) {
printf("%d ", i);
}
printf("\n");
return 0;
}
Here instead of printf(“%d\n”, i), we use printf(“%d “, i) to print each number followed by a space. After the loop, we add an extra printf(“\n”) to move to the next line. Now it will print all the numbers from 10 to 1 on the same line separated by spaces like: 10 9 8 7 6 5 4 3 2 1 Printing numbers on the same line with spacing can be useful when we want to display the output as a single sequence rather than multiple lines.
Nested Loops
We can also nest loops to decrement multiple counters simultaneously. For example:
#include <stdio.h>
int main() {
for(int i=2; i>0; i--) {
for(int j=5; j>0; j--) {
printf("%d %d ", i, j);
}
printf("\n");
}
return 0;
}
Here we have an outer for loop that decrements i from 2 to 1. Inside it, an inner for loop decrements j from 5 to 1. This will nested decrement two counters i and j and print them on separate lines like: 2 5 4 3 2 1 1 5 4 3 2 1 Nesting loops allows decrementing multiple variables concurrently in a controlled manner.
Conclusion
In this article, we discussed different loop constructs in C like for, while and do while loops to decrement a number from 10 to 1. The for loop provides the cleanest structure for fixed iterations with a counter variable. We also saw examples of printing the output on the same line, and nesting loops to decrement multiple variables simultaneously. Mastering loops is fundamental for writing efficient programs in C.