# number crunching

• 03-24-2003
Greenmonkey39
number crunching
i need to know how to input a number and print it with the numbers reversed like 123 to 321. :( I have not had any luck.
• 03-24-2003
Govtcheez
What have you tried?
• 03-24-2003
Prelude
>i need to know how to input a number and print it with the numbers reversed like 123 to 321.
Play around with the modulus operator and division and a loop. Or place the number in a string and print it out in reverse with a loop.

-Prelude
• 03-25-2003
Dev
Use somewhat stack mechanism.

Create a stack for holding integers.
Pass all digits in the number to the stack as they are encountered and then pop elements off the stack and print it.

Got it ?

If not then the best way is to use strings.

Accept input as a string say 'num'

then call strrev(num);

its simple.
• 03-25-2003
Dev
But you can't input big numbers due to limitation of integer size.

Try changing all int to long for big numbers.

#include <stdio.h>

/* To find length of number eg : 123 length 3 */
int len(int num)
{
int l=1;
while(num=num/10)
l++;
return l;
}
/* --------------------------------------------*/

int power(int num)
{
int ans=1;
while(--num)
ans*=10;
return ans;
}

int main()
{
int n,l,mul,copy,ans=0,rem;
printf("Enter a number : ");
scanf("%d",&n);
copy=n;
l=len(n);
mul=power(l);
do
{
rem=copy%10;
copy=copy/10;
ans+=mul*rem;
mul/=10;
}while(copy);

printf("\nOriginal Number : %d\nReverce Number : %d\n",n,ans);

getch();
}

In the mean time if somebody else has better code then this (I am sure it would be there ) then post it here.

As my program uses to many variables. I will also try finding some different logic to make this happen in small amount of code.
• 03-25-2003
Hammer
Please use code tags when posting code.

Why not code it like Prelude suggested. The code to do it that way can be written in just a few lines.
• 03-25-2003
Dev
I know that code can be written in very short using arrays.

But I think (THINK) that the way suggested by prelude would be to use modulo and division operator in a loop and printing out the reverse of the number digit by digit as calculation goes on.

But what if we need reverse of a number in further calculations in our program.

The code I have written doesn't use arrays or strings. And the output can be used by ohter parts of program also.

(Focusing on modularity not on getting results)

I am not a master in C. That's why i mentioned if somebody has made small program then post the code not algorithm.
• 03-25-2003
Hammer
Here are two ways:
Code:

```#include <stdio.h> void revprint(int i) {   while (i)   {     printf ("%d", i%10);     i /= 10;   }   putchar ('\n'); } int revint(int i) {   int newnum = 0;   while (i)   {     newnum = (newnum*10) + (i%10);     i /= 10;   }   return newnum; } int main(void) {   printf ("By revprint: 123 becomes ");   revprint(123);   printf ("By revint: %d becomes %d\n", 123, revint(123));   return 0; } /*  Output By revprint: 123 becomes 321 By revint: 123 becomes 321  *  */```
• 03-25-2003
Dev
Thanks for the code.

It requires practice to be at that level. Hope one day i will reach your level.