Related questions with answers
Question
(Display an integer reversed) Write a method with the following header to display an integer in reverse order:
public static void reverse(int number)
For example, reverse(3456) displays 6543 . Write a test program that prompts the user to enter an integer then displays its reversal.
Solution
VerifiedAnswered 2 years ago
Answered 2 years ago
Step 1
1 of 3To reverse a number we first initialize rev. We then add last digit from number to rev and remove that digit from number. We then multiply rev with 10 to add 0 at the end. We repeat the process until number reaches 0.
public static int reverse(int number){
// Initialize rev
int rev = 0;
// Repeat while number is greater than 0
while(number > 0){
// Add 0 to the end of rev
rev *= 10;
// Add last digit from number
rev += number % 10;
// Remove last digit in number
number /= 10;
}
// Return rev
return rev;
}
Create a free account to view solutions
By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Create a free account to view solutions
By signing up, you accept Quizlet's Terms of Service and Privacy Policy
Recommended textbook solutions

Intro to Java Programming, Comprehensive Version
10th Edition•ISBN: 9780133761313Y. Daniel Liang1,628 solutions

Fundamentals of Database Systems
7th Edition•ISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe961 solutions

Introduction to Algorithms
3rd Edition•ISBN: 9780262033848Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen872 solutions

Introduction to Algorithms
4th Edition•ISBN: 9780262046305Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen945 solutions
More related questions
1/4
1/7