Design a class Revno which reverses an integer number.
Example: 94765 becomes 56749 on reversing the digits of the number.
Some of the members of the class are given below:
Class name: Revno
Data member/instance variable:
num: to store the integer
Member functions/methods:
Revno(): default constructor
void inputnum(): to accept the number
int reverse(int nn): returns the reverse of a number by using recursive technique
void display(): displays the original number along with its reverse by invoking the method reverse()
Specify the class Revno, giving details of the constructor, void inputnum(), int reverse(int) and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.
import java.util.Scanner;
class Revno{
int num;
public Revno(){
num = 0;
}
public void inputnum(){
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
num = Integer.parseInt(in.nextLine());
}
public int reverse(int nn){
if(nn < 10)
return nn;
int c = String.valueOf(nn).length();
int p = (int)Math.pow(10, c - 1);
int d = nn % 10;
return d * p + reverse(nn / 10);
}
public void display(){
System.out.println("Original number: " + num);
System.out.println("Reverse = " + reverse(num));
}
public static void main(String[] args) {
Revno obj = new Revno();
obj.inputnum();
obj.display();
}
}