Friday 26 October 2012

menu driven shell

while [ 1 ]
do
echo " "
echo "1) Factorail no"
echo "2) Greatest of 3 nos"
echo "3) Prime no"
echo "4) Palindrome string"
echo "5) Palindrome no"
echo "6) Exit"
echo "Enter Your choice : "
read ch
case "$ch" in
1)
echo "Program for Factorial no"
echo "Enter number : "
read num
i=1
fact=1
for((i=1;i<=num;i++))
{
    fact=$(($fact*$i))
}
echo "Factorial is : " $fact
;;
2)
echo "Program for greatest no "
echo "Enter num1 : "
read n1
echo "Enter num2 : "
read n2
echo "Enter num3 : "
read n3
if [ $n1 -gt $n2 -a $n1 -gt $n3 ]
then
    echo "$n1 is greatest no"
elif [ $n2 -gt $n1 -a $n2 -gt $n3 ]
then
        echo "$n2 is greatest no"
else
    echo "$n3 is greatest no"
fi
;;
3)
echo "Program for prime no : "
echo "Enter num : "
read num
i=2
flag=1
for((i=2;i<=num-1;i++))
{
if [ $(($num % $i)) -eq 0 ]
then
    flag=0
    break
fi
}
if [ $flag -eq 1 ]
then
    echo "Prime no"
else
    echo "Not a Prime no"

fi
;;
4)
echo "Enter string : "
read str
reverse=`echo $str| rev`
if [ $reverse = $str ]
then
    echo "String is palindrome"
else
    echo "String is not palindome"
fi
;;
5)
echo "Enter number : "
read num
org=$num
rev=0
while [ $num -gt 0 ]
do
    r=$(($num%10))
    num=$(($num/10))
    rev=$(($rev*10))
    rev=$(($rev+$r))
   
done
if [ $rev -eq $org ]
then
    echo "No is palindrome"
else
    echo "No. is not palindrome"
fi
;;
6)
exit
;;
*)
echo "Invalid Choice"
;;
esac
done

No comments:

Post a Comment