Jump to content

Hello guys, i just want to ask you... how can i write code that will do this: User put some numbers (for example 123) and code will return 321.. i need to do it in Do-While cycle.. only thing that i know about this is that i can use 123% = 3 and then 12% = 2 .... 1% = 1 and than get it somehow together.

Link to comment
https://linustechtips.com/topic/567136-overturn-of-number-java-basic/
Share on other sites

Link to post
Share on other sites

int s = 0;
while(x)
{
s+=x%10;
x/=10;
}

And s will store your result. The x variable gets consumed in the process though.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×