. 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.Â
#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;
}
Comments
Leave a comment