1) Write a method that takes an integer value and returns the numberwith the dig
ID: 3613775 • Letter: 1
Question
1) Write a method that takes an integer value and returns the numberwith the digits reversed. For example, given the number 7631, themethod should return 1367. Incorporate this method into anapplication that reads a value from the user and displays theresult.
Explanation / Answer
The easiest way is to convert it to astring and reverse the characters. publicstatic int reverse(intinput) { String temp= ""+input; String reversed= ""; for(int i =temp.length-1; i>=0; i--) { reversed +=temp.charAtI(i); } returnInteger.parseInt(reversed); } Now you need to reverse a number from user input. publicstatic void main(String[]args) { // inputstream Scanner kb= newScanner(System.in); // prompt for userinput System.out.print("Enter aninteger:: "); int input= kb.nextInt(); // reversenumber int reverse= reverse(input); // output System.out.println(reverse); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.