Sunday 1 September 2013

How To Reverse An Integer In C


Here is a program for reversing the number.
First take the number as an input which you wanna reverse.
Then a while loop,which will continue as long as the number is greater than 0.
In the while loop the number is divided by 10 and the remainder is saved in the integer rem.
The value in the rem is printed.
And the number is divided by 10,the result is saves in integer num which can only save the int value so the floating point value will be discarded.
For Example
num=12345
In while loop : 12345%10=5
rem=5
12345/10=1234   ------Because the integer can not save floating point value.
This loop will continue until the whole number is reversed.
The last result will be 54321

No comments:

Post a Comment