JAVA:
Write a method with the following header to return a string format to represent the reverse order of the integer:
public static String reverse(int number)
For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal.
Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
import java.util.Scanner; public class ReverseInt { public static String reverseInt(int n){ String result = ""; while (n!=0){ result += n%10; n = n / 10; } return result; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter number:"); int n = scan.nextInt(); System.out.println("Reverse of "+n+" is "+reverseInt(n)); } }
Get Answers For Free
Most questions answered within 1 hours.