Asking you to provide source code in C++ that is commented, outlines file headin
ID: 3865812 • Letter: A
Question
Asking you to provide source code in C++ that is commented, outlines file headings (.cpp and .h), and shows sample output of working functionality. Program and requirements listed below:
PROGRAM
Write out a function that uses recursion in order to raise a number to a power. This function should accept two arguments. The first argument is the number to be raised and the second is the exponent. Make an assumption that the exponent is not a negative integer. Show that the function is correctly working for several values.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int findExponent(int x, int y)
{ if(y<0)
return -1;
if(y<=0)
return 1;
return findExponent(x,y-1)*x;
}
int main()
{
int x,y;
int ex;
cout<<"enter the numer: ";
cin>>x>>y;
while(y<0)
{
cout<<"Please enter positive number"<<endl;
cout<<"if you want then enter y/n";
char c;
cin>>c;
if(c=='n')
break;
cin>>y;
}
if(c=='n'){
cout<<"you have not enter correct input"<<endl;
return 0;
}
ex=findExponent(x,y);
cout<<"raising value is: "<<ex;
}
Ouptpu: if x=3, y=4
then output is 81;
if x=4, y=5
then output is 1024
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.