Write a recursive function power(base, exponent) that when invoked returns base^
ID: 3863395 • Letter: W
Question
Write a recursive function power(base, exponent) that when invoked returns base^exponent For example, power(3, 4) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. base^exponent = base * base^exponent-1 and the terminating condition occurs when exponent is equal to 1 because base^1 = base The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd that returns the greatest common divisor of X and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd(x, y) is x: otherwise gcd(x, y) is gcd(y, x% y), where is the remainder operator. Write a recursive function recursiveMinimum that takes an integer array and the array size as arguments and returns the smallest element of the array. The function should stop processing and return when it receives an array of one element. Write the int main(void) function as a driver program and call the above three functions result with sample input/output.Explanation / Answer
Output:
Enter base :
4
Enter Exponent:
3
power(4,3) = 64
Enter two positive integers to find GCD:
45
5
G.C.D of 45 and 5 is 5.
Enter size of array :
7
Enter 7 values into the array
12
3
1
6
8
3443
65
Minimum value is : 1
#include <stdio.h>
int power(int , int );
int GCD(int , int );
int main(void)
{
int base, exponent, output;
int n1, n2;
int size,array[100],i;
printf("Enter base : ");
scanf("%d",&base);
printf("Enter Exponent: ");
scanf("%d",&exponent);
output = power(base, exponent);
printf("power(%d,%d) = %d ", base, exponent, output);
printf("Enter two positive integers to find GCD: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d. ", n1, n2, GCD(n1,n2));
printf("Enter size of array : ");
scanf("%d",&size);
printf("Enter %d values into the array ",size);
for (i=0;i<size;i++)
{
scanf("%d",&array[i]);
}
printf("Minimum value is : %d ",Minimum(array,size));
return 0;
}
int power (int base, int exponent)
{
if (exponent)
{
return (base * power(base, exponent - 1));
}
return 1;
}
int GCD(int n1, int n2)
{
if (n2 != 0)
return GCD(n2, n1%n2);
else
return n1;
}
int Minimum(int array[] ,int size)
{
if (size == 0)
return array[0];
if(array[size-1] < Minimum(array , size -1))
return array[size -1];
else
return (Minimum(array , size -1));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.