-
Notifications
You must be signed in to change notification settings - Fork 10
London | 26-Java-June | Fatma Arslantas | Sprint 1 | Reverse Numbers #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # IntelliJ IDEA | ||
| .idea/ | ||
| *.iml | ||
|
|
||
| # Java build output | ||
| out/ |
| 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); | ||
| } | ||
|
|
||
| /** | ||
| * 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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, Would this be a good way to handle negative numbers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep nice one - this looks good to me!! |
||
| int digit = remaningNumber % 10; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks great 😄
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! |
||
| reversedNumber = reversedNumber * 10 + digit; | ||
| remaningNumber = remaningNumber / 10; | ||
| } | ||
|
|
||
| return reversedNumber; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice javadoc!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!