June 28th, 2013, 05:04 PM

Using a loop to reverse the order of digits provided as input
So here's how far I have gone:
Code:
#include <stdio.h>
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
do {
n = n % 10;
printf("The number reversed is: %d", n);
} while (n > 0);
return 0;
}
I am not sure what to do inside the 'do' statement. Please help me code this. Thank you :)
June 28th, 2013, 09:14 PM

Take the number 127 for example, to reverse the number you would have to first print out the number 7:
127 mod 10 = 7
Then you would print the number 2 :
((127 mod 100)  (127 mod 10)) / 10 = 2
And then you would print out the number 1 :
((127 mod 1000)  (127 mod 100)) / 100 = 1
So that`s 721.
or for another example lets take 1234:
1234 mod 10 = 4
((1234 mod 100)  (1234 mod 10)) / 10 = 3
((1234 mod 1000)  (1234 mod 100)) / 100 = 2
((1234 mod 10000)  (1234 mod 1000)) / 1000 = 1
That`s 4321.
Hope that was helpful ^^
June 28th, 2013, 09:17 PM

Originally Posted by KaiUR
Take the number 127 for example, to reverse the number you would have to first print out the number 7:
127 mod 10 = 7
Then you would print the number 2 :
((127 mod 100)  (127 mod 10)) / 10 = 2
And then you would print out the number 1 :
((127 mod 1000)  (127 mod 100)) / 100 = 1
So that`s 721.
or for another example lets take 1234:
1234 mod 10 = 4
((1234 mod 100)  (1234 mod 10)) / 10 = 3
((1234 mod 1000)  (1234 mod 100)) / 100 = 2
((1234 mod 10000)  (1234 mod 1000)) / 1000 = 1
That`s 4321.
Hope that was helpful ^^
I know what you are saying here. But I am not sure how to implement this as a code. That's what I am trying to know and thus I opened this thread.
June 29th, 2013, 08:44 AM

Alternative way to think
For example: 123 >321
Thinking
Input: 123
Round 1: calculate 123%10 = 3 ==> print 3
Round 2: calculate (123%100) / 10 = 2 ==> print 2
Round 3: calculate (123%1000) / 100 = 1 ==> print 1
Result: 321
Rethinking: Is there some link between each round?
(123%(10*(1))) / (1) = 3
(123%(10*(1*10))) / (1*10) = 2
(123%(10*(1*10*10))) / (1*10*10) = 1
You can see the key was in red. We name the key as 'a', and build an expression.
n = (123%(a*10))/(a) where a = 1, 10, 100
Mention that if a=1000, n/a = 0. It's a good point to stop.
Now, it is easy to write Cstatements:
Code:
n = (123%(10*a))/a;
a *= 10;
Repeating works quit good with doloop.
Just do statements, while (n/a) > 0 !
Code:
int number = 123;
int n = 0;
int a = 1;
do{
n = (number%(10*a))/a; // calculate n
a *= 10; // multiple a with 10
printf("%d", n); // print n to standard output
} while ((number/a) > 0); //if n/a > 0 repeat action, else exit loop
Same result with whileloop
Code:
int number = 123;
int n = 0;
int a = 1;
while ((number/a) > 0) { //if n/a > 0 entry loop, else exit loop
n = (number%(10*a))/a; // calculate n
a *= 10; // multiple a with 10
printf("%d", n); // print n to standard output
}
Same result with forloop
Code:
int number = 123;
for(int n = 0,int a = 1; (number/a) > 0; a *= 10){
n = (number%(10*a))/a; // calculate n
printf("%d", n); // print n to standard output
}
Write your expected output on a paper before write any Ccode!
Comments on this post
June 29th, 2013, 07:53 PM

Originally Posted by Homi@th
Alternative way to think
For example: 123 >321
Thinking
Input: 123
Round 1: calculate 123%10 = 3 ==> print 3
Round 2: calculate (123%100) / 10 = 2 ==> print 2
Round 3: calculate (123%1000) / 100 = 1 ==> print 1
Result: 321
Rethinking: Is there some link between each round?
(123%(10*
(1))) /
(1) = 3
(123%(10*
(1*10))) /
(1*10) = 2
(123%(10*
(1*10*10))) /
(1*10*10) = 1
You can see the key was in
red. We name the key as
'a', and build an expression.
n = (123%(a*10))/(a) where a = 1, 10, 100
Mention that if a=1000, n/a = 0. It's a good point to stop.
Now, it is easy to write
Cstatements:
Code:
n = (123%(10*a))/a;
a *= 10;
Repeating works quit good with
doloop.
Just do statements, while (n/a) > 0 !
Code:
int number = 123;
int n = 0;
int a = 1;
do{
n = (number%(10*a))/a; // calculate n
a *= 10; // multiple a with 10
printf("%d", n); // print n to standard output
} while ((number/a) > 0); //if n/a > 0 repeat action, else exit loop
Same result with
whileloop
Code:
int number = 123;
int n = 0;
int a = 1;
while ((number/a) > 0) { //if n/a > 0 entry loop, else exit loop
n = (number%(10*a))/a; // calculate n
a *= 10; // multiple a with 10
printf("%d", n); // print n to standard output
}
Same result with
forloop
Code:
int number = 123;
for(int n = 0,int a = 1; (number/a) > 0; a *= 10){
n = (number%(10*a))/a; // calculate n
printf("%d", n); // print n to standard output
}
Write your expected output on a paper before write any Ccode!
This is a really elegant way to approach this! Thanks a lot for your help! :)