Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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 n

Explanation / 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);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote