Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# IntelliJ IDEA
.idea/
*.iml

# Java build output
out/
28 changes: 28 additions & 0 deletions src/ReverseNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
public class ReverseNumbers {
public static void main(String[] args) {
int originalNumber = 3956;
int reversedNumber = reverseNumber(originalNumber);

System.out.println("Original number: " + originalNumber);
System.out.println("Reversed number: " + reversedNumber);
}

/**

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice javadoc!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

* Reverses the digits of a positive integer.
*
* @param number the number to reverse
* @return the number with its digits in reverse order
*/
private static int reverseNumber(int number) {
int reversedNumber = 0;
int remaningNumber = number;

while (remaningNumber > 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What changes would you have to make to get this working for negative numbers?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support negative numbers, I would handle the sign separately.

The current loop only works for positive numbers because it runs while remainingNumber > 0. For a negative number, I would first check whether the original number is negative, then reverse the absolute value of the number, and finally return the result as negative again if needed.

I could update the method like this:

private static int reverseNumber(int number) {
    boolean isNegative = number < 0;
    int remainingNumber = Math.abs(number);
    int reversedNumber = 0;

    while (remainingNumber > 0) {
        int digit = remainingNumber % 10;
        reversedNumber = reversedNumber * 10 + digit;
        remainingNumber = remainingNumber / 10;
    }

    if (isNegative) {
        return -reversedNumber;
    }

    return reversedNumber;
}

For example, -3956 would return -6593.

Would this be a good way to handle negative numbers?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep nice one - this looks good to me!!

int digit = remaningNumber % 10;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great 😄

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

reversedNumber = reversedNumber * 10 + digit;
remaningNumber = remaningNumber / 10;
}

return reversedNumber;
}
}