0% found this document useful (0 votes)
4 views

Program 11

Uploaded by

nandiswastik2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Program 11

Uploaded by

nandiswastik2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAM No.

:-

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 2 - Define class Palin with variables num and revnum

STEP 3 - Create constructor Palin() to initialize num and revnum to 0

STEP 4 - Define method accept() to read user input

STEP 5 - Create a Scanner object to read input from the console

STEP 6 - Print "Enter the number:"

STEP 7 - Read integer input into num

STEP 8 - Close the Scanner object

STEP 9 - Define method check() to check if the number is a palindrome

STEP 10 - Call reverse(num) and assign the result to revnum

STEP 11 - Print revnum and num

STEP 12 - If num is equal to revnum, print "Yes, It is a palindrome number."

STEP 13 - Else, print "No, It is not a palindrome number."

STEP 14 - Define method reverse(int y) to reverse the digits of a number


STEP 15 - If y divided by 10 equals 0, return the last digit (y % 10)

STEP 16 - Else, return (10 * (y % 10)) + reverse(y / 10)

STEP 17 - In the main method, create an object of Palin class (pl)

STEP 18 - Call accept() method on pl

STEP 19 - Call check() method on pl

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();
}
}

INPUT AND OUTPUT :-


VARIABLE DESCRIPTION TABLE :-

Name Type Scope Purpose


num int Class Holds the input number to check if it is a palindrome.
revnum int Class Stores the reversed number for comparison.
sc Scanner Method (accept) Reads user input from the console.
y int Method (reverse) Represents the current number being reversed.
pl Palin Method (main) Object of the Palin class to access its methods.

METHOD DESCRIPTION TABLE :-

Na Return Argument Argument Purpose


me Type Name Type
Pali N/A N/A N/A Constructor to initialize num and revnum.
n
acc void N/A N/A Reads user input for num.
ept
che void N/A N/A Checks if num is a palindrome and prints the result.
ck
reve int y int Recursively reverses the digits of a number.
rse
mai void args String[] Entry point of the program, handles user input and
n output.

You might also like