8:06 a.m.

(1) Comments

Reverse a number in C and Java

Jean-Michel

, ,

Stumble Upon Toolbar
On yahoo answer, i had to answer question like how to reverse number, so i desided to put it up
the guy who asked fisrt asked for a recursive function, so here it is




<-------C code ------->
#include
#include

int reverse( int iNumber )
{
if( iNumber >= 10 )
{
return (iNumber % 10) * pow(10, (int)log10((double)iNumber)) + reverse(iNumber / 10);
}
else
{
return iNumber;
}
}

int main()
{
printf ( "%d",reverse(123456789));
}

<------- Java code -------->

int reverse( int pNumber )
{
if( pNumber >= 10 )
{
return (int) ((pNumber % 10) * Math.pow(10, (int)Math.log10((double)pNumber)) + reverse(pNumber / 10));
}
else
{
return pNumber;
}
}


Jean-Michel
NJR-Team
Stumble Upon Toolbar
1 Response to "Reverse a number in C and Java"
Angelique S said :
September 16, 2009 at 11:03 a.m.
I found this blog from y & a. Hi.. I am Bazonggier Wisencare. Your answer is usefull. Thanks a lot.

Post a Comment

Have an opinion say it!