Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views2 pages

Assignment - 7

The document outlines a shell script assignment to reverse a given integer number. It includes an algorithm detailing the steps to extract and reverse the digits of the input number, as well as the corresponding code implementation. The discussion emphasizes the use of basic arithmetic operations and a while loop to achieve the reversal.

Uploaded by

SK IMAMUDDIN
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)
5 views2 pages

Assignment - 7

The document outlines a shell script assignment to reverse a given integer number. It includes an algorithm detailing the steps to extract and reverse the digits of the input number, as well as the corresponding code implementation. The discussion emphasizes the use of basic arithmetic operations and a while loop to achieve the reversal.

Uploaded by

SK IMAMUDDIN
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
You are on page 1/ 2

ASSIGNMENT – 7

Problem Statement: Write a shell script to reverse a given integer number.

Solution:

Algorithm:

1) Start
2) Prompt the user to input an integer.
3) Initialize a variable reverse = 0.
4) Repeat while the number num is greater than 0:
a. Extract the last digit: remainder = num % 10.
b. Append it to reverse: reverse = reverse * 10 + remainder.
c. Remove the last digit from num: num = num / 10.
5) Output the reversed number.

6) Stop

Code:
Output:
echo -n "Enter an integer: "
read num

reverse=0

while [ $num -gt 0 ]


do
remainder=$(( num % 10 ))
reverse=$(( reverse * 10 + remainder ))
num=$(( num / 10 ))
done

echo "Reversed number: $reverse"

Discussion: It takes an input number from the user and reverses its digits using basic
arithmetic operations and a while loop.

You might also like