Tuesday, March 13, 2012

Unix programs on simple logics

Unix program for amstrong number



The command given below is suitable for the computing in our saranathan college lab

echo "Enter the number"
read n
t=$n
s=0
b=0
c=10
while [ $n -gt $b ]
do
r=`expr $n % $c`
i=`expr $r \* $r \* $r`
s=`expr $s + $i`
n=`expr $n / $c`
done
echo $s
if [ $s -eq $t ]
then
echo "Amstrong number"
else
echo "Not an Armstrong number"
fi


Unix program for fibionacci series



The command given below is suitable for the computing in our saranathan college lab

echo "Enter the number"
read n
i=1
a=-1
fib=0
b=1
echo "Fibonacci series is"
while [ $i -lt $n ]
do
fib=`expr $a + $b`
a=$b
b=$fib
echo $fib
i=`expr $i + 1`
done


Unix program to check wheather the given number is prime or not



The command given below is suitable for the computing in our saranathan college lab

echo -n "Enter a number: "
read num
i=2

while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo "$num is not a prime number"
echo "Since it is divisible by $i"
exit
fi
i=`expr $i + 1`
done

echo "$num is a prime number "





No comments:

Post a Comment