0

So, I'm supposed to write a program determining Ermips. I've got the rest figured out, but I'm not sure how to reverse the number correctly. I'm supposed to use an array to reverse it.

For example, the number 357.

I use the mod operator to take the last digit and put it in the first index of the array.

357%10 = 7

myArray[0] = 7

357/10 = 35 for a remainder

Use the remainder 35 to start over.

35%10 = 3

myArray[1] = 3

35/10 = 3 for a remainder

etc. ...

I need to basically loop this so I can do any length number to reverse it.

Then, after I have that array, display the array to produce the number in reverse....753.

public class Reverse {
        public static void main(String[]args) {
        int n = 357;
        int MAX_NUMBERS = 20;
        int currentNumber = 0;
        int reverseNumber = 0;
        int remain = 0;
        int sum = 0;

                  int [] holdDigits = new int [MAX_NUMBERS];


        int exp = holdDigits.length;
        System.out.println("exp: " + exp);
        int index = 0;

                  //sum array
       int count = holdDigits.length;
       while (count > 0){
        holdDigits[index] = n%10;
        System.out.println(index + "index: " + holdDigits[index]);
        n = n/10;
        System.out.println("remainder: " + n);

        count--;

        index++;
        }

        while (index < holdDigits.length){
        reverseNumber += holdDigits[index]*Math.pow(10,count-exp);
        index--;
        System.out.println("sum so far: " + sum);
        }

    System.out.println("Number reversed: " + reverseNumber);
         }//end of main
    }//end of class

Totally figured it out now, thanks to Yogendra Singh ! Check it out:

    public class Reverse2 {

    public static void main(String[]args) {


    int n = 76495;
    int MAX_NUMBERS = 20;
    int reverseNumber = 0;
    int index = 0;  

    //declare an array to hold the digits while reversing
    int [] holdDigits = new int [MAX_NUMBERS];

    //the exponent is the number of spaced used in the array
    int exp = holdDigits.length;  

    //while the number is greater than 0, use mod to put the right-most
    //digit in index 0, divide the remaining number and increase the index
    //to put it in the next open slot of the array.
    while (n > 0){
        holdDigits[index] = n%10;
        n = n/10;
        index++;
    }

    //decrease the index by one so it doesn't add the remaining zero as
    //a placeholder in the number
    index--;

    //count is the index because below, you subtract it, making the display
    //of the array reversed.
    int count= index;

    //while the index is greater than zero, by starting at the last filled 
    //slot of the array, the reverse number is added onto each time by 
    //multiplying the number times 10 to the power of whichever place it
    //is which happens to be the index. 
    //EXAMPLE: to turn 7 into 700, multiply by 7x10^3
    while (index >= 0 ){
        reverseNumber += holdDigits[count-index]*Math.pow(10,index);

        //lower the index to do the next number of the array
        index--;
    }

    System.out.println("Reversed number: " + reverseNumber);


    }//end of main


}//end of class
0

2 Answers 2

1

There are some issues in the code as below:

  1. Run the first loop until remainder of division is 0
  2. Count the digits found in the division process
  3. Reduce the index by 1 after first loop as it is post incremented by one in the while loop

Sample corrected code could be as below:

    int exp = holdDigits.length;
    System.out.println("exp: " + exp);
    int index = 0;
    while (n > 0){
        holdDigits[index] = n%10;
        System.out.println(index + "index: " + holdDigits[index]);
        n = n/10;
        System.out.println("remainder: " + n);
        index++;
    }
    index--;
    int count= index;
    while (index >=0 ){
        reverseNumber += holdDigits[count-index]*Math.pow(10,index);
        index--;
        System.out.println("sum so far: " + sum);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

i apology if already you have got an answer but this is an short way to get reverse number using array with for loop.

    var val = prompt("enter number");
    var New = val.split("");
    var arr1 = [];
    console.log(New);
    for (i = New.length - 1; i >= 0; i--) {
        arr1 += New[i]  + ',';
    } console.log(arr1);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.