Tuesday, June 16, 2020

Write a program to compute a^n (a power n) using recursion.

INPUT & OUTPUT FORMAT:

Input consists of 2 integers

Refer sample input and output for formatting specifications

SAMPLE INPUT & OUTPUT:

Enter the value of a

2

Enter the value of n

8

The value of 2 power 8 is 256

SOLUTION:

#include<iostream>

using namespace std;

int pow(int n, int p){

  while(p>1){

    return n * pow(n,p-1);

  }

}

int main()

{

  cout<<"Enter the value of a"<<endl<<"Enter the value of n"<<endl;

  int n,p;

  cin>>n>>p;

  cout<<"The value of "<<n<<" power "<<p<<" is "<<pow(n,p);

  return 0;

}


No comments:

Post a Comment

horizontal ads