Shell Programming
1.program for ARMSTRONG number
Armstrong
Number
echo enter the number
read n
temp=$n
sum=0
while [ $n - gt
0 ]
do
r=`expr $n % 10`
sum=`expr
$sum
+ \( $r \*
$r \* $r \)`
n=`expr $n / 10`
done
if [ $temp
-eq $sum ]
then
echo $temp is armstrong
else
echo $temp is not armstrong
fi
exit 0
Output
1:
[cse3b520@CSE
~]$ sh a.sh
enter
the number
153
153 is
armstrong
2.program for FACTORIAL
echo
enter a number
read n
sum=1
while [ $n -gt 1 ]
do
sum=`expr
$sum \* $n`
n=`expr $n - 1`
done
echo $sum
exit 0
Output
1:
[cse3b520@CSE
~]$ sh n.sh
enter a
number
6
720
3.Program for LARGEST NUMBER
if [ $1 -gt $2 ]
then
x=$1
elif [ $2 -gt $3
]
then
x=$2
else
x=$3
fi
echo largest number is $x
exit 0
Output
1:
[cse3b520@CSE
~]$ sh g.sh 12 34 45
largest
number is 45
4.Program for PERFECT NUMBER
echo
enter a number
read no
i=1
ans=0
while [ $i -le `expr
$no / 2` ]
do
if [ `expr
$no % $i` -eq 0 ]
then
ans=`expr
$ans + $i`
fi
i=`expr $i + 1`
done
if [ $no -eq $ans
]
then echo $no
is perfect
else
echo $no is not perfect
fi
Output
1:
[cse3b520@CSE ~]$ sh p.sh
enter a
number
34
34 is
not perfect
5. Program for REVERSE
echo
enter a num
read num
rev=0
while [
$num -gt 0 ]
do
k=`expr $num \%
10`
l=`expr $rev \* 10`
rev=`expr
$l + $k`
num=`expr
$num / 10`
done
echo
reverse number is $rev
exit 0
Output
1:
[cse3b520@CSE ~]$ sh r.sh
enter a
num
12345
reverse
number is 54321
6.
Program for EVEN OR ODD
echo enter a number
read n
i=1
while [ $i -le $n ]
do
echo
enter term
read x
if [ `expr
$x \% 2` -eq 0 ]
then
echo $x is even
else
echo $x is odd
fi
i=`expr $i + 1`
done
Output
1:
[cse3b520@CSE
~]$ sh eo.sh
enter a
number
4
enter
term
3
3 is odd
enter
term
2
2 is
even
enter
term
7
7 is odd
enter
term
8
8 is
even
7. Program for FIBONACCI
echo
enter the number of terms
read n
f1=0
f2=1
i=1
echo $f1
echo $f2
while [ $i -le $n ]
do
f3=`expr
$f1 + $f2`
f1=$f2
f2=$f3
i=`expr $i + 1`
echo $f3
done
exit 0
Output
1:
[cse3b520@CSE
~]$ sh fb.sh
enter
the number of terms
5
0
1
1
2
3
5
8
8. Program for PALINDROME
echo
enter a num
read num
rev=0
temp=$num
while [ $num -gt
0 ]
do
k=`expr $num \%
10`
l=`expr $rev \*
10`
rev=`expr
$l + $k`
num=`expr
$num / 10`
done
if [ $rev -eq
$temp ]
then
echo $temp is a palindrome
else
echo $temp is not a palindrome
fi
exit 0
Output
1:
[cse3b520@CSE
~]$ sh fbb.sh
enter a
num
234
234 is
not a palindrome
9. Program for prime or not prime
echo
enter the number
read n
count=0
i=1
while [ $i -le
$n ]
do
if [ `expr
$n % $i` -eq 0 ]
then
count=`expr
$count + 1`
fi
i=`expr $i + 1`
done
if [ $count
-eq 2 ]
then
echo $n is prime
else
echo $n is
not prime
fi
Output
1:
[cse3b520@CSE ~]$ sh p1.sh
enter
the number
3
3 is
prime
10. Program for SUM OF N NUMBERS
echo
enter the number
read n
i=1
sum=0
while [ $i -le $n ]
do
sum=`expr
$sum + $i`
i=`expr $i + 1`
done
echo sum
of numbers : $sum
exit 0
Output
1:
[cse3b520@CSE ~]$ sh sum.sh
enter
the number
20
sum of
numbers : 210