#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2

    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. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep 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.

    Hope that was helpful ^^
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    40
    Rep Power
    19
    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!

    Comments on this post

    • arman.khandaker agrees
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    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! :)

IMN logo majestic logo threadwatch logo seochat tools logo