हिंदी

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: - Computer Science (Theory)

Advertisements
Advertisements

प्रश्न

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: Renvo
Data member/instance variable:  
num: to store the integer number
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.

कोड लेखन
Advertisements

उत्तर

import java.util*;
public class Revno
{
    int num;
    public Revno()
    {
      num=0;
    }
    public void inputnum()
    {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number");
      num=in.nextInt();
    }
    public void Reverse(int num)
    {
      if (num < 10)
    {
      System.out.println(num);
      return;
    }
    else
    {
      System.out.print(num % 10);
      Reverse(num / 10);
    }
   }
   public void display()
   {
      System.out.println("Original    Number
      :"+num);
      System.out.println("Reversed Number :");
      Reverse(num);
    }
    public static void main(String args[])
    {
    Revno obj=new Revno();
    obj.inputnum();
    obj.display();
  }
}
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2021-2022 (March) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×