Question:

Write a program to reverse a number

Program:

Explanation:

The while loop contains the statements to isolate or separate the digits of a number ,which is then multiplied to 10 and sums the reminder. Thus you get the reverse.

Let’s try it out

Say n=366

while val>0//True, then reminder is 6,reverse also 6 and val is 36

while 36>0//True, then reminder is 6, reverse is (6*10)+6 =66, and val is 3

while 3>0// True, then reminder is 3,reverse is (66*10)+3=663 and val is 0

Hence reverse is 663

Output:

 

Leave a Reply