Program 11
Program 11
:-
PROBLEM DEFINITION :-
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
‘revnum’ using 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(). Need to write main() and variable description or
comment lines.
ALGORITHM :-
STEP 1 - START
STEP 20 – END
PROGRAM CODE :-
import java.util.*;
public class Palin {
int num;
int revnum;
Palin() {
num = 0;
revnum = 0;
}
void accept() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number :");
num = sc.nextInt();
sc.close();
}
void check() {
revnum = reverse(num);
if (num == revnum)
System.out.println("Yes, It is a palindrome no.");
else
System.out.println("No, It is not a palindrome no.");
}
int reverse(int y) {
if (y / 10 == 0) {
revnum = (revnum * 10) + (y % 10);
return revnum;
} else {
revnum = (revnum * 10) + (y % 10);
return reverse(y / 10);
}
}
public static void main(String[] args) {
Palin pl = new Palin();
pl.accept();
pl.check();
}
}