bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To Reverse a Number

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java How To Reverse a Number?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ num = 1234;

Reverse a Number

Take an integer and print it in reverse order:

Example

int num = 1234;
int reversed = 0;
while (num != 0) {
  int digit = num % 10;
  reversed = reversed * 10 + digit;
  num /= 10;
}
System.out.println("Reversed: " + reversed);

Explanation: We start with the number 1234 . Inside the loop: - num % 10 gives us the last digit (4, then 3, then 2, then 1). - We add this digit to reversed , making the new number grow step by step. - num /= 10 removes the last digit from the original number. When the loop finishes, reversed contains 4321 .

Previous

Java How To Find Even or Odd Numbers

Next

Java How To Find Positive or Negative Numbers