Thursday, November 1, 2012

How to find the number is Armstrong or not in Unix


#################################################
# To find the number is Armstrong or not                   #
#################################################

#!/bin/ksh
echo "This program is to find whether given number is Armstrong number or not”

# To make sure sufficient arguments
if [ $# -ne 1 ]
 then
   echo "Please provide one argument"
exit -1
fi

# Declaring and Assigning the values to the local variables
a=$1
s=0

# Logical part to find the Armstrong number
while [ $a -gt 0 ]
do
b=$(( $a%10 ))
s=$(( $s+$b*$b*$b ))
a=$(( $a/10 ))
done
if [ $s -eq $1 ]
then
 echo "The given number is the Armstrong number"
else
 echo "The given number is not Armstrong number"
fi
exit 0

How to find the sum of digits in the number in Unix


###########################################################
# To find the sum of digits in the number                 #
###########################################################

#!/bin/ksh

# Checking the required arguments
if [ $# -ne 1 ]
 then
   echo "Please provide one argument"
exit -1
fi

# assigning the arguments to the local variables
a=$1
s=0

# Logical part to find the sum of digits in the number
while [ $a -gt 0 ]
do
b=$(( $a%10 ))
s=$(( $s+$b ))
a=$(( $a/10 ))
done

echo "The sum of digits in $1 is $s"
exit 0

How to find whether string is palindrome or not in Unix


#############################################################
# To find whether the string is palindrome or not                                            #
#############################################################

#!/bin/ksh

# To make sure the sufficient arguments
if [ $# -ne 1 ]
then
  echo "Please enter one argument"
 exit -1
fi

# Declaring and assigning the values to the local variables
b=`echo $1 | wc -c `
c=$(( $b-1 ))
d=$(( $c/2 ))
flag=1
m=1

# Logical part to handle the palindrome calculations
while [ $m -le $d ]
do
    e=`echo $1 | cut  -c$c`
    f=`echo $1 | cut  -c$m`
    if [ $e != $f ]
      then
          flag=0
          break
    fi
     c=$(( $c-1 ))
     m=$(( $m+1 ))
done

  if [ flag -eq 0 ]
 then
echo "The string is not a palindrome"
else
echo "The string is palindrome"
fi
exit 0