How about:
for (unsigned i = n ; i-- > 0 ; )
{
// do stuff with i
}
Answer from Skizz on Stack Overflow Top answer 1 of 16
119
How about:
for (unsigned i = n ; i-- > 0 ; )
{
// do stuff with i
}
2 of 16
15
for ( unsigned int loopIndex = n; loopIndex > 0; --loopIndex ) {
unsigned int i = loopIndex - 1;
...
}
or
for ( unsigned int loopIndex = 0; loopIndex < n; ++loopIndex ) {
unsigned int i = n - loopIndex - 1;
...
}
YouTube
youtube.com โบ watch
Reverse for Loop in C - C Programming Tutorial 51 ๐ - YouTube
Reverse for Loop in C - C Programming Tutorial 51 ๐โบ Master the Reverse for Loop in C! Learn how to use for loops with decrementing counter variables, and e...
Published ย April 18, 2020
forward and reverse for loops with C++ c - C++ Forum
Not as elegant as the other for loops, but it does work. ... FYI, Visual Studio 2019, build 16.11.2 requires the C++ language standard to be set to /std:c++latest to compile. Set the language standard to C++20 and VS spits out the following errors: While searching the bowels of the interwebs to find out the possibilities of a reverse ... More on cplusplus.com
c - iterating an array backwards in For loop condition to stop at 0 when using unsigned integers causing infinite loop - Stack Overflow
So the starting index should be ... 0, the for loop will not print anything, because i-1 == SIZE_MAX. ... The problem is that j >= 0 is always true because j is unsigned. When counting down to zero with an unsigned, I usually use postfix --: ... Unsigned value wraps around so when j == 0 and the loop does j--, j >= 0 is still true. ... Will print the string in reverse order : ... More on stackoverflow.com
How do you write a reverse loop in C using while, do - while or/and for loop?
I don't mean to sound like a dick, but if this is the type of question you turn to an online forum for, are you sure you want to be learning C? This sounds like a first assignment in a C programming class. There's nothing wrong with asking for help when you're stuck, but you haven't said what part you don't understand, which makes me think you were given an assignment and didn't read or listen to what you were supposed to. My very first job, 24 years ago, was tutoring C programming and I can guarantee that if you don't make an effort to read and understand the materials on your own, you're not going to get very far as a programmer. If you're just taking a mandatory class and don't care about learning C maybe you'll muddle through, but you'll annoy a whole bunch of people online by having them do your entire semester's assignments. If you google 'learn C' you'll get a number of good tutorial sites. Some of them let you edit and run code so you can tinker and make sure you understand what you're doing. If you've never programmed in any language before, make sure that you understand first what loops are and why you use them, and look at the differences between the types of loops in C. If you have prior programming experience (we learned to do reverse loops in BASIC on the Apple II in elementary school and the concept is the same) and just need to understand C's syntax, take a look at one of the online language references. If you're stuck, you can post "I'm trying to do ___ but I don't understand how ____ ...". It gets old having a bunch of posts that basically say "here's my homework, do it for me." More on reddit.com
Is there a proper way to do a reverse for loop?
var i: usize = len; while (i > 0) { i -= 1; // do something } nothing dirty about while loops, that's some organic, gluten-free machine code right there. Healthy CPUs love that shit. More on reddit.com
Videos
04:36
C program to print digits 1 to 5 in reverse order using a for loop ...
00:41
Program to reverse string using for loop | C programming #shorts ...
02:13
C++ Program to Reverse a Number using for loop - YouTube
08:54
Reverse number program using for loop | C++ Tutorial for Beginners ...
22:29
Forward for Loop in C - C Programming Tutorial 50 - YouTube
02:01
C Program to Print Natural Numbers from 1 to N In Reverse Order ...
Ashn
ashn.dev โบ blog โบ 2022-08-21-c-reverse-for-loop-iteration.html
Reverse For-Loop Iteration In C - ashn
August 21, 2022 - One can reverse-iterate from N-1 down to 0 in C using a for-loop of the form:
w3resource
w3resource.com โบ c-programming-exercises โบ for-loop โบ c-for-loop-exercises-37.php
C Program: Display the number in reverse order - w3resource
September 27, 2025 - printf("Input a number: "); // Prompt the user to input a number. scanf("%d", &num); // Read the input from the user. for(t = num; num != 0; num = num / 10){ // Loop to reverse the digits of the number. r = num % 10; // Extract the last digit (remainder when divided by 10). sum = sum * 10 + r; // Build the reversed number by adding the extracted digit. } printf("The number in reverse order is : %d \n", sum); // Print the reversed number. } ... Write a C program to reverse a number using recursion instead of loops.
Java2s
java2s.com โบ Code โบ C โบ Language-Basics โบ Reverseorderofforloop.htm
Reverse order of for loop : For ยซ Language Basics ยซ C / ANSI-C
Reverse order of for loop : For ยซ Language Basics ยซ C / ANSI-C
TutorialsPoint
tutorialspoint.com โบ learn_c_by_examples โบ reverse_counting_program_in_c.htm
Reverse Counting program in C
procedure counting() FOR value = END to START DO DISPLAY value END FOR end procedure ยท Now, we shall see the actual implementation of the program โ ยท #include <stdio.h> int main() { int i, start, end; start = 1; end = 10; //reverse counting, we'll interchange loop variables for(i = end; i >= start; i--) printf("-\n", i); return 0; }
Forgetcode
forgetcode.com โบ c โบ 1913-reverse-the-string-using-for-loop
Reverse The String Using FOR Loop in C - Forget Code
Forget Code launches a powerful ... Loop #include <stdio.h> #include <string.h> int main(void) {char *str="ForgetCode"; printf("Reverse the String:"); for(int i=(strlen(str)-1);i>=0;i--) { printf("%c",str[i]); } return 0; }...
GeeksforGeeks
geeksforgeeks.org โบ c language โบ c-program-to-traverse-an-array-in-reverse
C Program to Traverse an Array in Reverse - GeeksforGeeks
July 23, 2025 - Recursion can also be used to traverse an array in reverse order. It is less efficient than looping due to the overhead of function calls and additional memory usage. Below is the approach to use recursion to traverse the array in revese: Define a recursive function that takes the array and the current index as arguments. The base case is when all elements are traversed. Recursively call the function for ...
Cplusplus
cplusplus.com โบ forum โบ general โบ 279811
forward and reverse for loops with C++ c - C++ Forum
(I'm including SSCCE [ ... a container. I'll use a std::vector to illustrate 1. The old-school C/C++ for loop: ... Yet there is no way to do a reverse range-based for loop....
Medium
travcav.medium.com โบ why-reverse-loops-are-faster-a09d65473006
A look at reverse loops - Trav Cav - Medium
June 19, 2017 - Remember, always code for readability first, trust your compiler, and if you attempt any of the following make sure you benchmark. Reverse loops have been around a long time but have mostly been forgotten or ignored because these days nobodyโs going to miss those extra few clock cycles and ...
Quora
quora.com โบ How-do-you-reverse-a-number-in-a-for-loop
How to reverse a number in a for loop - Quora
So we exit the loop. Now if we see the value that is stored in rev, we have 54321 which is the desired output. Hope this helps ! ... Write A Book Now With Qyx AI Book Creator! Write entire books quickly with Qyx AI Book Creator. World's most powerful and affordable AI ghostwriter. ... have been doing it for two decades when I joined quora ยท Author has 3.9K answers and 23.9M answer views ยท 6y ยท Originally Answered: How do I write a C program to reverse ...
Tutorjoes
tutorjoes.in โบ c_programming_tutorial โบ reverse_number_using_for_loop_in_c
Print the Reverse Number Using For Loop in C Programming
"printf" function to print the current value of n on each iteration of the loop, and decrement the value of n by 1 in each iteration. Finally, the program returns 0 to indicate that the program has completed successfully. #include<stdio.h> int main() { int n,i=1; printf("\nEnter the limit:"); scanf("%d",&n); for(n;n>=i;n--) { printf("\n%d",n); } return 0; } To download raw file Click Here
Top answer 1 of 7
11
for(j=i; j>0; j--) {
printf("%c", str[j-1]);
}
Would be another option.
For a beginner maybe easier to understand.
But the other answers would be better.
edit: i'd say the best would be for(j=i; j-- > 0;) by l3x.
decrementing j after checking if it is greater then 0.
Using a do{}while() loop would also work.
j = i;
do {
j--;
printf("%c", str[j]);
} while (j > 0);
2 of 7
7
size_t is an unsigned integer and it's never going to be less than 0. So
the condition in for loop is always true:
for(j=i;j>=0;j--)
You can modify the condition to (a bit ugly though):
for(j=i; j-- > 0;){
...
}
Note that in your condition, you are printing the \0 null byte too, which is a non-printable character. (since the j starts with a value equal to the string length). The above condition takes care of that too.
Also:
- you can use
strlen()instead of looping over it yourself. - Check the return value of
scanf()if input reading wad successful.
Sankalandtech
sankalandtech.com โบ Tutorials โบ C โบ reverse-number-for-loop-c.html
a program in c programming to print the reverse of any given number using a for loop.
Also in mathematical calculations and scientific simulations reversing number can help examine and understand composite data sets with accuracy and precision. This program includes taking any number from the user as input and then use the for loop to reverse the digits of the number and show the reverse number as output using printf() function.
TutorialKart
tutorialkart.com โบ c-programming โบ how-to-reverse-a-number-using-loops-in-c
How to Reverse a Number using Loops in C
February 18, 2025 - To reverse a number in C using loops, we repeatedly extract the last digit using the modulus operator (%), add it to the reversed number, and remove the last digit using division (/). This process continues until all digits are processed.
TutorialKart
tutorialkart.com โบ c-programming โบ how-to-reverse-a-string-using-loops-in-c
How to Reverse a String using Loops in C
February 18, 2025 - We create an empty array rev[] to store the reversed string. Using a for loop, we iterate from the last character of str to the first and copy each character to rev[].
Reddit
reddit.com โบ r/c_programming โบ how do you write a reverse loop in c using while, do - while or/and for loop?
r/C_Programming on Reddit: How do you write a reverse loop in C using while, do - while or/and for loop?
July 8, 2017 -
How would you execute the number from 1 to 10 reversed?
Top answer 1 of 3
42
I don't mean to sound like a dick, but if this is the type of question you turn to an online forum for, are you sure you want to be learning C? This sounds like a first assignment in a C programming class. There's nothing wrong with asking for help when you're stuck, but you haven't said what part you don't understand, which makes me think you were given an assignment and didn't read or listen to what you were supposed to. My very first job, 24 years ago, was tutoring C programming and I can guarantee that if you don't make an effort to read and understand the materials on your own, you're not going to get very far as a programmer. If you're just taking a mandatory class and don't care about learning C maybe you'll muddle through, but you'll annoy a whole bunch of people online by having them do your entire semester's assignments. If you google 'learn C' you'll get a number of good tutorial sites. Some of them let you edit and run code so you can tinker and make sure you understand what you're doing. If you've never programmed in any language before, make sure that you understand first what loops are and why you use them, and look at the differences between the types of loops in C. If you have prior programming experience (we learned to do reverse loops in BASIC on the Apple II in elementary school and the concept is the same) and just need to understand C's syntax, take a look at one of the online language references. If you're stuck, you can post "I'm trying to do ___ but I don't understand how ____ ...". It gets old having a bunch of posts that basically say "here's my homework, do it for me."
2 of 3
1
Or using a while loop: int i = 11; while (i-- > 1) { printf("%d\n", i); }
Tomclarke
tomclarke.dev โบ home โบ programming
Reverse Iteration with Range-Based For Loops in C++ โ tomclarke.dev
A fairly minimal example, but for most cases this works beautifully! With the above code, iterating in reverse is as simple as it is forwards: // range-based for loop in reverse for (auto& i : reversed (values)) { std::cout << i; }