Imagine you need to locate a ^ b. The easiest method is to multiply a by b times using a loop.
- Let a ^ b be the input. The base is a, while the exponent is b.
- Start with a power of 1.
- Using a loop, execute the following instructions b times
- power = power * a
- The power system has the final solution, a ^ b.
Let's enhance our comprehension of the aforementioned method by examining a C program example:
# include < stdio . h >
# include < string . h >
# include < conio . h >
# include < math >
# include < stdlib >
int Pow ( int a , int b ) {
int power = 1 , i ;
for ( i = 1 ; i < = b ; + + i ) {
power = power * a ;
}
return power ;
}
int main ( ) {
long long int base , exponent ;
printf ( " enter Base : " ) ;
scanf ( " % d " , & base ) ;
printf ( " enter Power : " ) ;
scanf ( " % d " , & exponent ) ;
printf ( " % d ^ % d = % d " , base , exponent , Pow ( base , exponent ) ) ;
}
Output:
Enter Base: 5
Enter Power: 3
5 ^ 3 = 125
..........................
Process executed in 3.22 seconds
Press any key to continue.
Explanation
The code snippet mentioned exhibits an O(N) time complexity, where N represents the exponent. The space complexity is denoted by O(1).
Using While loop:
# include < stdio . h >
# include < string . h >
# include < conio . h >
# include < math >
# include < stdlib >
int main ( )
{
int n , exp , exp1 ;
long long int value = 1 ;
printf ( " enter the number and its exponential : \ n \ n " ) ;
scanf ( " % d % d " , & n , & exp ) ;
exp1 = exp ; // storing original value for future use
// same as while ( ( - - exp ) ! = - 1 )
while ( exp - - > 0 )
{
value * = n ; // multiply n to itself exp times
}
printf ( " \ n \ n % d ^ % d = % l l d \ n \ n " , n , exp1 , value ) ;
return 0;
}
Output:
enter the number and its exponential :
5
4
5 ^ 6 = 625
..........................
Process executed in 0.11 seconds
Press any key to continue.
Explanation
Long Long Int is double the size of Long Int. The format specifier for long long int is percent lld.
Using Recursion to find the Power of Given Integer
Assuming that the input is a raised to the power of b, the exponent of 'a' will increment by one during each iteration. In order to calculate a raised to the power of b, the recursive function is called b times.
Therefore, the recursive function Pow(a, b) is employed to compute a raised to the power of b.
The function follows a simple logic: if b is equal to 0, it returns 1; otherwise, it recursively calls Pow(a, b-1) and multiplies the result by a.
Let's enhance comprehension of the aforementioned method by illustrating it through a C program example:
# include < stdio . h >
# include < string . h >
# include < conio . h >
# include < math >
# include < stdlib >
int Pow ( int a , int b ) {
if ( b = = 0 )
return 1 ;
else
return Pow ( a , b - 1 ) * X ;
}
int main ( ) {
long long int base , exponent ;
printf ( " enter Base : " ) ;
scanf ( " % d " , & base ) ;
printf ( " enter Power : " ) ;
scanf ( " % d " , & exponent ) ;
printf ( " % d ^ % d = % d " , base , exponent , Pow ( base , exponent ) ) ;
return 0;
}
Output:
Enter Base: 5
Enter Power: 4
5 ^ 4 = 625
..........................
Process executed in 1.22 seconds
Press any key to continue.
Explanation:
In the provided C code example, the time complexity is O(N) and the space complexity is also O(N) due to the internal stack usage.