Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Saturday, August 9, 2014

Shell programs for prime and palindrome

Shell scripting programs to implement whether a number is prime as well as a program to check for a palindrome. If you have any doubts please let me know 

PRIME
#!/bin/bash
echo " enter a number"
read n
i=2
flag=0
while((i<=$n/2))
do
((c = n % i))
if ((c==0))
then
flag=1
fi
((i=i+1))
done
if ((flag==0))
then
echo "prime"
else
echo "composite"
fi



PALINDROME
echo “enter a string to be entered:”
read str
echo
len=`echo $str | wc –c`
len= ((len-1))
i=1
j= ((len/2))
while((i<j))
do
k=`echo $str | cut –c $i`
l=`echo $str | cut –c $len`
if((k!=l))
then
flag=1;
fi
((i=i+1))
((len=len-1))
Done
If((flag==0))
then
Echo “palindrome”
Else
Echo “ not palindrome”

fi