Reputation:
So I'm trying to reverse a number in java using a forloop, I get the right value but I'm not sure if thats the best way of doing it.
package forloops;
/*
% prints the last number in the sequence
/ prints every number except for the last one
*/
public class modulusForLoops {
public static void main(String[]args) {
int orig = 123456789;
int num = orig;
for (int i = 0; i < 1; i++) {
num = orig % 10; //9
int secondDigit = orig / 10; //12345678
int secondDigitPrinted = secondDigit % 10; //8
int thirdDigit = secondDigit / 10; //1234567
int thirdDigitPrinted = thirdDigit % 10; //7
int fourthDigit = thirdDigit / 10; //123456
int fourthDigitPrinted = fourthDigit % 10; //6
int fifthDigit = fourthDigit / 10; //12345
int fifthDigitPrinted = fifthDigit % 10;
int sixthDigit = fifthDigit / 10; //1234
int sixthDigitPrinted = sixthDigit % 10; //4
int seventhDigit = sixthDigit / 10; //123
int seventhDigitPrinted = seventhDigit % 10; //3
int eigthDigit = seventhDigit / 10; //12
int eigthDigitPrinted = eigthDigit % 10; //2
int lastDigit = eigthDigit / 10; //1
System.out.println(orig + " reversed is " + num + secondDigitPrinted + thirdDigitPrinted + fourthDigitPrinted + fifthDigitPrinted + sixthDigitPrinted + seventhDigitPrinted + eigthDigitPrinted + lastDigit);
}
}
}
Upvotes: 0
Views: 1453
Reputation: 773
Let's stick with the logic you have in mind to reverse any number. For better understanding, let's list out the algorithm steps that you are using.
Repeat below steps until there are no digits left in the given number:
I) get the last digit from the number, i.e. lastDigit = number % 10
II) remove the last digit from the number, i.e. numberWithoutLast = number / 10
When we want to go through a sequence of steps multiple times, i.e. repeat them, we make use of the looping structures like for, while or do...while
Therefore, if we were to rewrite your program-the loop part-it would be as follows:
public static void main(String[] ar) {
int orig = 123456789;
int lastDigit = 0;
/* we'll use the copy of original number for step I & II
* instead of messing with the original number
*/
int numberWithoutLast = orig;
String reversed = ""; // we'll use this to store every last digit
for(int i = 0;
i < Integer.toString(orig).length(); /* this will repeat the loop for number of digits in "orig" */
i++) {
lastDigit = numberWithoutLast % 10;
reversed += Integer.toString(lastDigit);
numberWithoutLast = numberWithoutLast / 10;
}
// lastly we print the reversed number
System.out.println("Reversed Number: " + reversed);
}
This was the manual way of reversing an integer. For an automatic way, you can have a look at @Andreas's answer.
Upvotes: 0
Reputation: 7618
Just in case you want to know how to do it by modulus and loop. The idea is to pop the unit digit from the source and push it to the destination in every iteration, in a number way.
int orig = 123456789; //assume > 0
int num = 0;
for(int temp = orig;temp > 0;temp/=10)
{
num = num * 10 + temp % 10;
}
System.out.println(orig + " reversed is " + num);
Upvotes: 0
Reputation: 2521
You could simply convert it to String
and using java.lang.StringBuilder
reverse the string.
int orig = 123456789;
String numString = Integer.toString(orig);
String reversed = "";
for (int i = numString.length() - 1; i >= 0; i--) { // loop through the string from back to front
reversed += numString.charAt(i); // add each character to the resulting string
}
System.out.println(reversed);
Or alternatively
int orig = 123456789;
String numString = Integer.toString(orig); // convert int to String
String reversed = new StringBuilder(numString).reverse().toString(); // reverse string
System.out.println(reversed);
Upvotes: 1