Making a number in reverse using for loop java












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(Stringargs) {
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);
}
}
}









share|improve this question























  • 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.

    – Geno Chen
    Nov 28 '18 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.

    – Ben Avery
    Nov 28 '18 at 3:35











  • Oh thanks. Maybe I could use a string?

    – user10113279
    Nov 28 '18 at 3:38
















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(Stringargs) {
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);
}
}
}









share|improve this question























  • 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.

    – Geno Chen
    Nov 28 '18 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.

    – Ben Avery
    Nov 28 '18 at 3:35











  • Oh thanks. Maybe I could use a string?

    – user10113279
    Nov 28 '18 at 3:38














0












0








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(Stringargs) {
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);
}
}
}









share|improve this question














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(Stringargs) {
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);
}
}
}






java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 '18 at 3:28







user10113279




















  • 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.

    – Geno Chen
    Nov 28 '18 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.

    – Ben Avery
    Nov 28 '18 at 3:35











  • Oh thanks. Maybe I could use a string?

    – user10113279
    Nov 28 '18 at 3:38



















  • 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.

    – Geno Chen
    Nov 28 '18 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.

    – Ben Avery
    Nov 28 '18 at 3:35











  • Oh thanks. Maybe I could use a string?

    – user10113279
    Nov 28 '18 at 3:38

















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.

– Geno Chen
Nov 28 '18 at 3:34





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.

– Geno Chen
Nov 28 '18 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.

– Ben Avery
Nov 28 '18 at 3:35





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.

– Ben Avery
Nov 28 '18 at 3:35













Oh thanks. Maybe I could use a string?

– user10113279
Nov 28 '18 at 3:38





Oh thanks. Maybe I could use a string?

– user10113279
Nov 28 '18 at 3:38












3 Answers
3






active

oldest

votes


















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.






share|improve this answer































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





    share|improve this answer


























    • Edited my answer to include for-loop

      – Andreas
      Nov 28 '18 at 3:54











    • Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.

      – Sikorski
      Nov 28 '18 at 3:56











    • 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.

      – Andreas
      Nov 28 '18 at 3:59













    • Read the class name in his program, and he's using mod in code

      – Sikorski
      Nov 28 '18 at 5:05





















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





    share|improve this answer
























    • I figured it out before thanks.

      – user10113279
      Nov 28 '18 at 4:35











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511663%2fmaking-a-number-in-reverse-using-for-loop-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer




























      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.






      share|improve this answer


























        0












        0








        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 4:42









        Dhruv JoshiDhruv Joshi

        6625




        6625

























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





            share|improve this answer


























            • Edited my answer to include for-loop

              – Andreas
              Nov 28 '18 at 3:54











            • Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.

              – Sikorski
              Nov 28 '18 at 3:56











            • 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.

              – Andreas
              Nov 28 '18 at 3:59













            • Read the class name in his program, and he's using mod in code

              – Sikorski
              Nov 28 '18 at 5:05


















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





            share|improve this answer


























            • Edited my answer to include for-loop

              – Andreas
              Nov 28 '18 at 3:54











            • Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.

              – Sikorski
              Nov 28 '18 at 3:56











            • 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.

              – Andreas
              Nov 28 '18 at 3:59













            • Read the class name in his program, and he's using mod in code

              – Sikorski
              Nov 28 '18 at 5:05
















            1












            1








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





            share|improve this answer















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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 28 '18 at 3:53

























            answered Nov 28 '18 at 3:43









            AndreasAndreas

            2,05931223




            2,05931223













            • Edited my answer to include for-loop

              – Andreas
              Nov 28 '18 at 3:54











            • Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.

              – Sikorski
              Nov 28 '18 at 3:56











            • 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.

              – Andreas
              Nov 28 '18 at 3:59













            • Read the class name in his program, and he's using mod in code

              – Sikorski
              Nov 28 '18 at 5:05





















            • Edited my answer to include for-loop

              – Andreas
              Nov 28 '18 at 3:54











            • Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.

              – Sikorski
              Nov 28 '18 at 3:56











            • 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.

              – Andreas
              Nov 28 '18 at 3:59













            • Read the class name in his program, and he's using mod in code

              – Sikorski
              Nov 28 '18 at 5:05



















            Edited my answer to include for-loop

            – Andreas
            Nov 28 '18 at 3:54





            Edited my answer to include for-loop

            – Andreas
            Nov 28 '18 at 3:54













            Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.

            – Sikorski
            Nov 28 '18 at 3:56





            Converting into string defeats the purpose of modulus and for loops which I believe is OP's homework.

            – Sikorski
            Nov 28 '18 at 3:56













            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.

            – Andreas
            Nov 28 '18 at 3:59







            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.

            – Andreas
            Nov 28 '18 at 3:59















            Read the class name in his program, and he's using mod in code

            – Sikorski
            Nov 28 '18 at 5:05







            Read the class name in his program, and he's using mod in code

            – Sikorski
            Nov 28 '18 at 5:05













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





            share|improve this answer
























            • I figured it out before thanks.

              – user10113279
              Nov 28 '18 at 4:35
















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





            share|improve this answer
























            • I figured it out before thanks.

              – user10113279
              Nov 28 '18 at 4:35














            0












            0








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





            share|improve this answer













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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 28 '18 at 4:15









            Ricky MoRicky Mo

            1,7471212




            1,7471212













            • I figured it out before thanks.

              – user10113279
              Nov 28 '18 at 4:35



















            • I figured it out before thanks.

              – user10113279
              Nov 28 '18 at 4:35

















            I figured it out before thanks.

            – user10113279
            Nov 28 '18 at 4:35





            I figured it out before thanks.

            – user10113279
            Nov 28 '18 at 4:35


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511663%2fmaking-a-number-in-reverse-using-for-loop-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Lallio

            Unable to find Lightning Node

            Futebolista