Answer to Question #289976 in C++ for ali

Question #289976

. Write a program that asks for a number and a power. Write a recursive function that takes the number to the power. Thus, if the number is 2 and the power is 4, the function will return 16. 


1
Expert's answer
2022-01-24T08:04:35-0500
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

#include <iostream>

using namespace std;

/*
	Write a program that asks for a number and a power. 
	Write a recursive function that takes the number to the power. 
	Thus, if the number is 2 and the power is 4, the function will return 16. 
*/


int PowerCalc(int b, int p)
{
    if (p != 0)
        return (b*PowerCalc(b, p-1));
    else
        return 1;
}


int main()
{
    int b, p, result;


    cout << "\n\tEnter Base  Number                  : ";    cin >> b;
    cout << "\n\tEnter Power Number(positive integer): ";    cin >> p;


    result = PowerCalc(b, p);
    cout <<"\n\tResult: "<<b <<"^"<<p<< " = " << result;


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog