Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 28th, 2013, 05:04 PM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
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

#2
June 28th, 2013, 09:14 PM
 KaiUR
Registered User

Join Date: Jun 2013
Posts: 2
Time spent in forums: 30 m 36 sec
Reputation Power: 0
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.

#3
June 28th, 2013, 09:17 PM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
Quote:
 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.

#4
June 29th, 2013, 08:44 AM
 Homi@th
Contributing User

Join Date: Jun 2013
Posts: 40
Time spent in forums: 1 Day 8 h 58 m 5 sec
Reputation Power: 18
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

Re-thinking: 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 C-statements:
Code:
```n = (123%(10*a))/a;
a *= 10;```

Repeating works quit good with do-loop.
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 while-loop
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 for-loop
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 C-code!
arman.khandaker agrees!

#5
June 29th, 2013, 07:53 PM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
Quote:
 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 Re-thinking: 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 C-statements: Code: ```n = (123%(10*a))/a; a *= 10;``` Repeating works quit good with do-loop. 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 while-loop 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 for-loop 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 C-code!

This is a really elegant way to approach this! Thanks a lot for your help!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Using a loop to reverse the order of digits provided as input