0

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);
    }
}
}
3
  • It is the worst way of doing it. You are limited to fixed length number, and the for loop have run just once. So you may roll your similar statement into one, and use for to unroll them. Commented Nov 28, 2018 at 3:34
  • Looks like you are doing a homework task. I won't provide you with an answer but will try and point you in the right direction.The way you are doing it is not correct at all the for loop is essentially pointless. You might want to think about how you could iterate over each number. You might need to put the number into a different data structure. Commented Nov 28, 2018 at 3:35
  • Oh thanks. Maybe I could use a string? Commented Nov 28, 2018 at 3:38

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Edited my answer to include for-loop
Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.
I believe modulus is not the main requirement since OP has even considered using string in the question's comment. In addition to that, modulus has never been mentioned in the question, and it is implied that OP is asking for a better way of doing it. As for for-loop, I already include that in the answer.
Read the class name in his program, and he's using mod in code
0

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.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.