Saturday, December 1, 2012

Shell Script to find "a" to the power "b" using function


Q. How do I write Shell Script function to find "a" to the power "b"
Ans.
#!/bin/bash
#function to find "a" to the power "b"
power()
{
num=$1
pow=$2
counter=1
result=1
if((pow==0)); then
result=1
fi
if ((num==0)); then
result=0
fi
if((num>=1&&pow>=1)); then
while((counter<=pow))
do
result=$((result*num))
counter=$((counter + 1))
done
fi
#Printing the result
echo "$1 to the power $2 is $result"
}

0 comments:

Post a Comment