Reversing digits of an integer

Reversing digits of an integer


Problem Definition: Reversing the digit of an integer.

Problem Analysis: Given a integer number, abcd (say 1234where a=I, b=2andso on), the algorithm should convert the number to dcba.

Solving by Example: Consider an integer number 7823.

The reverse of an integer number is obtained by successively dividing the integer by 10 and accumulating the remainders till the quotient becomes less than 10.

The remainder of a division operation can be found out by the mod function.



Accumulated
Quotient
7823 Mod 103
782
782 Mod 10
3278
78 Mod 10
328
7
7 Mod 10
3287
0


Algorithm Definition


Step 1: Start

Step 2: read the integer n to be reversed.

Step 3: Initialize rev to zero.

Step 4: Write 'n' is greater than 0, does the following step:

(a) Divide the integer n by 10

(b) Store the quotient to n.

(c) Multiply 10 to the already existing value of rev.

(d) Add the remainder to the value of rev.

Step 5: Print the value of rev.

Step 6: Stop

Programs