Thread: Reverse number program

1. Reverse number program

If we know logic how do we write code for it

For an example

quotient = 987 / 10 --> 98
remainder = 987 % 10 --> 7 -> first number
quotient = 98 / 10 --> 9 --> third number
remainder = 98 % 10 --> 8 --> second number

first ->second -> third = 7 8 9

I do not want the code, I want to find out way how I can write my code if I know the logic.

2. If you know your logic, then the only way you can code it is by starting to type, I guess?

3. Originally Posted by Zeus_
If you know your logic, then the only way you can code it is by starting to type, I guess?
Normally I know How do the Reverse Number. The problem is that how do I implement the program ?

Code:
```#include<stdio.h>

int main ()
{
int number = 987;
int reminder;
int reverse = 0;

reminder = number % 10; //  remainder = 987 % 10 --> 7 -> first number

number = number / 10; // quotient = 987 / 10 --> 98
reminder = number % 10; //remainder = 98 % 10 --> 8 --> second number

number = number / 10; // quotient = 987 / 10 --> 98 quotient = 98 / 10 --> 9 --> third number

return 0;
}```

4. Do you know about while loops?

Now would be a good time to find out.

As in, while you have some number > 0, you do your % and / thing.

5. Originally Posted by Salem
you do your % and / thing.
I am using % and / operator

Code:
```#include<stdio.h>
int main ()
{
int number = 987;
int reminder;
int reverse = 0;

while ( number > 0 )
{
reminder = number % 10; //  remainder = 987 % 10 --> 7 -> first number
number = number / 10; // quotient = 987 / 10 --> 98
reminder = number % 10; //remainder = 98 % 10 --> 8 --> second number
number = number / 10; // quotient = 987 / 10 --> 98 quotient = 98 / 10 --> 9 --> third number
}
return 0;
}```
What will be the algorithm inside of the while loop

6. Try it with only one % and one / inside the loop.

Maybe just this to begin with.
Code:
```while ( number > 0 ) {
printf("Number=%d\n", number);
number = number / 10;
}```

7. Originally Posted by Salem
Try it with only one % and one / inside the loop.

Maybe just this to begin with.
Code:
```#include<stdio.h>
int main ()
{
int number = 987;
int reminder;
int reverse = 0;

while ( number > 0 ) {
reminder = number % 10; //  remainder = 987 % 10 --> 7 -> first number
printf("Number=%d\n", number);
number = number / 10;
//   reminder = number % 10; //  remainder = 987 % 10 --> 7 -> first number
printf("reminder =%d\n", reminder);

}

return 0;
}```
Number=987
reminder =7
Number=98
reminder =8
Number=9
reminder =9

8. > reminder =7
> reminder =8
> reminder =9
Good, now can you put them all into reverse ?

9. Originally Posted by Salem
> reminder =7
> reminder =8
> reminder =9
Good, now can you put them all into reverse ?
I tried but I do not understand how to do that

reverse = reverse * 10 + reminder

11. Originally Posted by Salem
reverse = reverse * 10 + reminder
I got it

Code:
```#include<stdio.h>

int main ()
{
int number = 987;
int reminder;
int reverse = 0;

while ( number > 0 ) {

reverse = reverse * 10 + number % 10;
number   = number / 10;

}

printf("Reverse number is = %d \n", reverse);

return 0;
}```
Reverse number is = 789

12. Originally Posted by Parth12
I got it
One little thing, thou... this:
Code:
```#include <stdio.h>

int main( void )
{
int number = -987;
int reverse = 0;

while ( number > 0 )
{
reverse = reverse * 10 + number % 10;
number   = number / 10;
}

printf( "Reverse number is = %d \n", reverse );
}```
Will result in 0!

Popular pages Recent additions