recursive program to raise number to power

I have to write two files, one is a header and the other a file that calls that header to run the equation. The problem I am having is that I do not see a response.

Any help would be appreciated.

Header file named header.h

#include <iostream>
#include <iomanip>
using namespace std;
float power(float a, int n)


{
{
if (a==0);

return (0);
}
{
if(n==0);

return (1);
}
{
if (n>0);

return( a* power(a,n-1));
}

}


#include <iostream>
#include <iomanip>
#include "header.h"
int x;
int main(void)
{
float a;

int n;

cout << "Enter base as an integer: ";
cin >> a;

cout << "Enter exponent as an integer: ";
cin >> n;

cin >> x;

}
you've got some syntax issues here... check out the beginners guides to c++ on this site for how to correctly use if conditionals and other basics.

for starters you've got something like

1
2
3
4
5
{
if (a==0);

return (0);
}


where you should have something along the lines of

1
2
3
4
if( a == 0 )
{
  return 0;
}


also your main function never calls the power function. it should look something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include "header.h"
int x;
int main(void)
{
float a;

int n;

cout << "Enter base as an integer: ";
cin >> a;

cout << "Enter exponent as an integer: ";
cin >> n;

cout << "\nResult: " << power(a,n) ;


cin >> x; //if your on windows you might want a System("pause"); instead of this line

}
Topic archived. No new replies allowed.