LINUX SEHLL PROGRAM FOR FINDING SUM OF DIGITS AND REVERSE OF A NUMBER | OPERATING SYSTEM (OS) LAB IN LINUX ENVIRONMENT

AIM:
To write a shell program for the following:
(I) Sum of digits.
(II) Reverse of a number.
ALGORITHM:
Sum of digits:
· Get the given number n.
· Separate the digits of n individually.
· Then add the individual digit.
Reverse of a number:
· Get the given number n.
· Separate the digits of n individually.
· Then add the individual digits and print the sum.
· Rearrange the digit in reverse order.

PROGRAM SOURCE CODE
(i) Sum of digits:

echo ”enter the no”
read n
s=0
while [ $n – gt 0 ]
do
t= `expr $n % 10 `
s= `expr $s + $t `
n=`expr $n/10`
done
echo “sum of digits is $s”


(ii) Reverse of a number:

echo “enter the number”
Read n
s=0
While [ $n –gt 0 ]
do
r=`expr $n % 10`
n=`expr $n / 10`
s=`expr $s \* 10 +$r`
done
echo “the reverse of the given number is $s”


CONCLUSION:
Thus the shell program to find the sum of digit of a number are written and executed successfully.


Related post