How to reverse a number using array? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

How to reverse a number using array?

11th Oct 2016, 5:22 PM
Raj shah
Raj shah - avatar
9 Réponses
+ 1
Two ways: - Figure out each individual digit, put them into an array, then reverse them - Convert the number to string, reverse the string, convert the string back to a number The second version is easier so let's get that out of the way first: int number = 12345; int reversed = Integer.parseInt(Integer.toString(number).reverse()); First we call Integer.toString to convert the number to a string, then we call .reverse() on that to reverse the string, and then we punch that into Integer.parseInt to make a number out of the string again. But that's cheating, let's do it the "proper" way! I'll tell you how you'd accomplish that without giving away the code :D Say you have a number, 12345. We need to get one digit seperated from the rest. The obvious choices would be to either split 1 from 2345, or 1234 from 5. Turns out the second one is easier, since it's just dividing by 10! 12345 : 10 = 1234 5 Remainder Easy enough. Java can both divide and calculate remainders. Something like this would do it: int division = number / 10; int remainder = number % 10; That digit you can push onto your array. What's left to do? easy - take 1234 and do the same thing, that is split it into 123 and 4 using division. Then you keep doing that until there is no more number left. Then you take what is in the array and turn it into a number again :) You are probably going to need some sort of while loop, maybe two!
11th Oct 2016, 10:04 PM
Schindlabua
Schindlabua - avatar
0
Will you a give me a detailed answer
11th Oct 2016, 6:25 PM
Raj shah
Raj shah - avatar
0
Mmm maybe i don't understand your doubt! Can you repeat all? And why you what use array?
11th Oct 2016, 6:32 PM
Riccardo S
Riccardo S - avatar
0
I wanna know how to use array function
11th Oct 2016, 6:35 PM
Raj shah
Raj shah - avatar
0
And i m just at basic level only so i don't know much about it
11th Oct 2016, 6:36 PM
Raj shah
Raj shah - avatar
0
Mmm no using array for this thing is inconvenient! For reverse a number you can use a ready-use method like: VariableInStrinh.Reverse();
11th Oct 2016, 6:50 PM
Riccardo S
Riccardo S - avatar
0
Array.Reverse([your digit as array]);
11th Oct 2016, 7:26 PM
Ksenia
Ksenia - avatar
0
int num= 123; char[] numArr = (""+num).ToCharArray(); string reversed = ""; make a for loop with a counter that starts with numArr.Length()-1, decrements by 1 as long as the variable's value is >= 0. using that loop, access the array using the counter's name as index and append it to the reversed variable. Than lastly, convert the reversed variable back to integer using int.Parse() function
12th Oct 2016, 3:43 PM
Erwin Mesias
Erwin Mesias - avatar
- 1
IMHO I will use a for loop. But in reverse: For(x=var.Lenght; x>0;x--) Is this work?
11th Oct 2016, 6:22 PM
Riccardo S
Riccardo S - avatar