0% found this document useful (0 votes)
35 views4 pages

Shell Script for Palindrome Check

The document provides a shell script that prompts the user to enter a number, reverses it, and checks if it is a palindrome. It includes steps for creating and editing the script using the vi editor, making it executable, and running it. The script uses a while loop to reverse the number and compares the original and reversed values to determine if the number is a palindrome.

Uploaded by

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

Shell Script for Palindrome Check

The document provides a shell script that prompts the user to enter a number, reverses it, and checks if it is a palindrome. It includes steps for creating and editing the script using the vi editor, making it executable, and running it. The script uses a while loop to reverse the number and compares the original and reversed values to determine if the number is a palindrome.

Uploaded by

benassure7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

5.

Write a shell script to reverse a given number and check whether it is


palindrome or not.

STEP 1: CREATE AND EDIT FILE USING vi EDITOR

STEP 2: CREATE SCRIPT


#!/bin/bash

# Prompt user to enter a number


echo "Enter a number:"
read num

# Store original number for later comparison


original=$num
reverse=0

# Reverse the number


while [ $num -gt 0 ]
do
digit=$(( num % 10 ))
reverse=$(( reverse * 10 + digit ))
num=$(( num / 10 ))
done

# Print the reversed number


echo "Reversed number is: $reverse"
# Check if the number is a palindrome
if [ $original -eq $reverse ]
then
echo "$original is a Palindrome number."
else
echo "$original is Not a Palindrome number."
fi
STEP 4: CHANGE THE FILE INTO EXECUTABLE
chmod +x [Link]
STEP 5: RUN THE SCRIPT
EXPLAINATION:
echo "Enter a number:"
read num

Prompts the user to enter a number.


The read command takes the input and stores it in a variable named num.

bash
original=$num
reverse=0

original stores the original value of num (because num will be modified during reversal).
reverse is initialized to 0. It will be used to store the reversed number.

bash
while [ $num -gt 0 ]
do
digit=$(( num % 10 ))
reverse=$(( reverse * 10 + digit ))
num=$(( num / 10 ))
done

This is the main logic that reverses the number:


while [ $num -gt 0 ]: Runs the loop while num is greater than 0
digit=$(( num % 10 )): Gets the last digit of the number using modulo.
reverse=$(( reverse * 10 + digit )): Builds the reversed number step by step.
num=$(( num / 10 )): Removes the last digit of num by dividing it by 10.
For example, if input is 123:
Step 1: digit = 3, reverse = 0 * 10 + 3 = 3
Step 2: digit = 2, reverse = 3 * 10 + 2 = 32
Step 3: digit = 1, reverse = 32 * 10 + 1 = 321

bash
echo "Reversed number is: $reverse"
Prints the reversed number for the user to see.

bash
if [ $original -eq $reverse ]
then
echo "$original is a Palindrome number."
else
echo "$original is Not a Palindrome number."
Fi

This checks if the original number is equal to the reversed number.


If yes, it's a palindrome.
Otherwise, it's not a palindrome.

You might also like