Different algorithm for reversing a number using expanded form of numbers

Viewed 85

Below is the code for reversing a number (in the standard way)

#include <stdio.h>
int main() 
{
  int result=0;
  int q,n,rem;

  printf("enter number: ");
  scanf("%d",&n);
  q=n;

  while(q!=0){
    rem=q%10;
    result=result*10+rem;
    q=q/10;
  }

  printf("reversed number is: %d",result);
    return 0;
}

But I was thinking whether there is a way to find the reversed program using the expanded form of numbers?

For example: If the input to the program is 123, then the required output would be 321 which can be written as 3 * 100+2 * 10+1 * 1

3 Answers

There is no particular logic like that,if you are interested to acheive that kind of output, you can just come up with something like this

#include <stdio.h>
#include <math.h>
int main() 
{
  int result=0;
  int q,n,rem;

  printf("enter number: ");
  scanf("%d",&n);
  q=n;
  int number[100];
   for (int i = 0; i < 100 ; i++){
         number[i] = -1;
   }
  int i=0;
  while(q!=0){
    rem=q%10;
    number[i++]= rem;
    q=q/10;
  }
  int size=0;
    for (i = 0; i < 100 ; i++){
         if(number[i]== -1) break;
         else{
            size++;
         }
   }
   int tenPowers= size;
  for(int i=0; i<=size-1 && tenPowers>=0 ;i++){
      printf("%dx%d", number[i],(int)pow(10,tenPowers-1));
      tenPowers=tenPowers-1;
      if(tenPowers>=0) printf("+");
  }

    return 0;
}

You can have this. It's not reversing, but it's formatting the output for you. Reversing a binary number or a string is not difficult.

int main() {
    int n = 123456;
    char in[16], obuf[] = " + x*10000000";

    sprintf( in, "%d", n );
    int rev = strlen( in ) + 2;
    for( int o=3, i=0; (obuf[3] = in[i]) != '\0'; o=0, i++ )
        printf( "%.*s", rev+3-o-i-(in[i+1]?0:2), obuf+o );

    return 0;
}

Output

1*100000 + 2*10000 + 3*1000 + 4*100 + 5*10 + 6

You can expand the sizes to suit your needs.

I'm not quite sure what you mean by a "different algorithm for reversing a number using expanded form of numbers."

Perhaps this is a solution to what you are asking:

Take each digit, one at a time, moving from right to left across the number and multiply that digit by the multiple of 10 associated with the current left-most digit. Accumulate these values in the variable reverse.

e.g. number = 123, digitCount = 3, power = 100, reverse = 0

 for loop executed digitCount times (3 times)
   get current right-most digit (3)
   multiply current right-most digit (3) times current left-most multiple of 10 (100) = 300
   reverse = 300
   drop right-most digit from number (number changes from 123 to 12)
   adjust multiple of 10 (power changes from 100 to 10)
   continue with second pass through loop, etc.

Also your version of the program will not properly handle trailing zeroes. The printf statement at the end of this program will fill in any formerly trailing zeroes which now should be leading zeroes.

/* reverse.c

   reverse the digits of a non-negative integer and display it on the terminal
   uses an advanced formatting option of printf() to handle any trailing zeroes
   e.g. 500 produces 005    

*/

#include <stdio.h>

int main (void)
{
    printf("\n"
           "program to reverse a number\n"
           "\n"
           "enter a number: ");
    int number;
    scanf ("%d", &number);
    printf("\n");

    int digitCount = 1;
    int power = 1;
    while (number / power > 9) {
        ++digitCount;
        power *= 10;    
    }

    // power = multiple of 10 matching left-most digit in number    
    // e.g. if number = 123 then power = 100

    int reverse = 0;
    for (int i = 1; i <= digitCount; ++i) {
        reverse += number % 10 * power;       // number % 10 = right-most digit
        number /= 10;                         // drop right-most digit  
        power /= 10;                          // adjust multiple of 10
    }

    // .* represents a variable that specifies the field width (the minimum number of
    // digits to display for an integer and with unused digits filled by leading zeroes)
    printf("reversed number: %.*d\n", digitCount, reverse);

    return 0;
}
Related