Page 447 - Computer science 868 Class 12
P. 447
}
9. A class Palin has been defined to check whether a positive number is a Palindrome number or not. [ISC 2017]
The number ‘N’ is palindrome if the original number and it’s reverse are the same.
Some of the members of the class are given below:
Class name : Palin
Data members/instance variables:
num : integer to store the number
revnum : integer to store the reverse of the number
Methods/Member functions:
Palin() : constructor to initialize data members with legal initial values
void accept() : to accept the number
int reverse(int y) : reverses the parameterized argument ‘y’ and stores it in revenue using a
recursive technique
void check() : checks whether the number is a Palindrome by invoking the function reverse()
and display the result with an appropriate message
Specify the class Palin giving the details of the constructor (), void accept(), int reverse(int) and void check(). Define the main()
function to create an object and call the functions accordingly to enable the task.
Ans. import java.util.*;
public class Palin {
int num, revnum;
static Scanner x = new Scanner(System.in);
Palin() {
num = 0;
revnum = 0;
}
void accept()
{
System.out.println("Enter a number");
num = x.nextInt();
}
int reverse(int y)
{
if (y > 0)
{
revnum = revnum * 10 + y % 10;
return reverse(y / 10);
}
else
return revnum;
}
void check()
{
int p = num;
if (num == reverse(p))
System.out.println("palindrome");
else
System.out.println("not a palindrome");
}
public static void main(String[] args)
{
Palin obj = new Palin();
obj.accept();
obj.check();
}
}
445
Recursion 445

