We would like to 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 and displays its reversal.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Create Scanner System.out.println("Display and integer reversed:"); System.out.print("Enter an integer: "); int number = input.nextInt(); reverse(number);//from ww w.j av a 2 s. c om } /** Reverse Method displays an integer in reverse order */ public static void reverse(int number) { //your code here } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Create Scanner System.out.println("Display and integer reversed:"); System.out.print("Enter an integer: "); int number = input.nextInt(); reverse(number); } /** Reverse Method displays an integer in reverse order */ public static void reverse(int number) { while (number > 0) { System.out.print((number % 10)); number /= 10; } System.out.println(); } }
Another solution
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int entry = input.nextInt(); reverse(entry);/*from w w w .j a va2 s . c o m*/ } public static void reverse(int number) { StringBuilder s = new StringBuilder(number + ""); System.out.println(s.reverse()); } }