Программа Bash для поиска A в степени B
Опубликовано: 16 Февраля, 2022
Учитывая два числа A и B, напишите сценарий оболочки, чтобы найти значение A B.
Примеры:
Вход : 2 4 Выход : 2 в степени 4 равно 16 Вход : 1 0 Выход : 1 в степени 0 равно 1
Рекомендуется: сначала попробуйте свой подход в {IDE}, прежде чем переходить к решению.
Подход
A to the power B (AB) means multiplying A, B times. Using a naive approach to solve this problem.
For Example:-
A = 2 and B = 3
A3 = 8 which comes by multiplying 2 3 times i.e. 2*2*2 = 8 .
BASH
# Bash Program to find# A to the power B # Subroutine to find A# to the power Bpow(){ # 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 # inputA=2B=4 # calling the pow functionpow $A $B |
Output:
16