C program :You are now allowed to use the following in additional to the techniq
ID: 3722437 • Letter: C
Question
C program :You are now allowed to use the following in additional to the techniques of the previous chapters: function prototypes functions Full math.h library Random functions rand() and srand() Seed random number generator with and time() Recursion!
Q3: (Recursive Exponentiation) (34 points) Write a recursive function power( base, exponent ) that when invoked returns baseexponent For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step would use the relationship base^exponent = base * base^exponent–1 and the terminating condition occurs when exponent is equal to 1 because base^1 = base Your program should prompt for the base and exponent
Explanation / Answer
CODE
#include <stdio.h>
long power( long base, long exponent );
int main()
{
long b;
long e;
printf( "Enter a base and an exponent: " );
scanf( "%ld%ld", &b, &e );
printf( "%ld raised to the %ld is %ld ", b, e, power( b, e ) );
return 0;
}
long power( long base, long exponent )
{
if ( exponent == 1 ) {
return base;
}
else
{
return base * power( base, exponent - 1 );
}
}
Sample output
Enter a base and an exponent: 5 10
5 raised to the 10 is 9765625
Coded as required if any queries please get back to me
Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.