The Java code is right here to reverse the stuffs.
- import java.util.Scanner;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class Reverse {
- public static void main(String args[]) throws Exception {
- BufferedReader br = new BufferedReader(new InputStreamReader(System. in ));
- System.out.println("1-Want to reverse a number..?");
- System.out.println("2-Want to reverse a string..?");
- System.out.println();
- System.out.print("Select any option: ");
- int select = Integer.parseInt(br.readLine());
- switch (select) {
- case 1:
- int n, reverse = 0;
- System.out.print("Enter the number to reverse: ");
- Scanner in = new Scanner(System. in );
- n = in .nextInt();
- while (n != 0) {
- reverse = reverse * 10;
- reverse = reverse + n % 10;
- n = n / 10;
- }
- System.out.println("Reversed number is: " + reverse);
- break;
- case 2:
- String str, reverse1 = "";
- Scanner input = new Scanner(System. in );
- System.out.print("Enter a string to reverse: ");
- str = input.nextLine();
- int length = str.length();
- for (int i = length - 1; i >= 0; i--)
- reverse1 = reverse1 + str.charAt(i);
- System.out.println("Reversed string is: " + reverse1);
- break;
- default:
- System.out.println("Wrong choice :(");
- }
- }
- }
Thank you, keep learning and sharing.