Question : The problem of finding the value of 2 raised to an integer power of n
ID: 3877951 • Letter: Q
Question
Question : The problem of finding the value of 2 raised to an integer power of n, or 2n, where n is a non-negative integer, can be expressed in the following mathematical form,
For example, 22 = 2 × 21 = 2 × 2 × 20 = 4.
(a) Write a recursive function power2 that computes and returns the value of 2 raised to a power of n, where n is the only function argument. The function prototype is given as: int power2(int);
(b) Write a complete C program to test the recursive function that you have written in part (a). Prompt user for n.
12x2 2 × 2n if nExplanation / Answer
#include<stdio.io>
using namespace std;
int power2(int n)
{
if(n==0)
return 1;
else
return (2*power2(n-1));
}
int main(){
int n,result;
printf(" enter the number: ");
scanf("%d",&n);
result=power2(n);
printf(" result is %d ",&result);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.