Bash 程序找到 A 的幂 B
给定两个数字 A 和 B 编写一个 shell 脚本来查找 A B的值。
例子:
Input :
2 4
Output :
2 to the power 4 is 16
Input :
1 0
Output :
1 to the power 0 is 1
方法
A 的 B 次幂 (A B ) 表示乘以 A、B 次。使用幼稚的方法来解决这个问题。
例如:-
A = 2 和 B = 3
A 3 = 8 乘以 2 3 倍即 2*2*2 = 8 。
BASH
# Bash Program to find
# A to the power B
# Subroutine to find A
# to the power B
pow()
{
# value of A
a=$1
# value of B
b=$2
# c to count counter
c=1
# res to store the result
res=1
#
if((b==0));
then
res=1
fi
if((a==0));
then
res=0
fi
if((a >= 1 && b >= 1));
then
while((c <= b))
do
res=$((res * a))
c=$((c + 1))
done
fi
# Display the result
echo "$1 to the power $2 is $res"
}
# Driver Code
# input
A=2
B=4
# calling the pow function
pow $A $B
Output:16