Posts

Showing posts from February 24, 2015

Program to find whether a number is special or not in Linux/Unix

echo "Enter a number "
read n
s=0
num=$n
r=0
while [ $n != 0 ]
do
r=` expr $n \% 10 `
fact=1
nu=$r
while [ $nu -ge 1 ]
do
fact=` expr $fact \* $nu `
nu=` expr $nu - 1 `
done
s=` expr $s \+  $fact `
n=` expr $n \/ 10 `
done
if [ $s -eq $num ]
then
echo "Special"
else
echo "Not a Special"
fi

Program to find whether a number is magic or not in Linux/Unix

echo "Enter a number "
read n
s=0
num=$n
r=0

while [ $n -gt 9 ]
do
s=0
while [ $n -gt 0 ]
do
r=` expr $n \% 10 `

s=` expr $s \+  $r `

n=` expr $n \/ 10 `
done
echo "$s"
n=$s
done

if [ $n -eq 1 ]
then
echo "Magic Number"
else
echo "Not a Magic Number"
fi