• 0

[C++] pow


Question

Sorry to post this annoying simple question but it's starting to aggravate me..

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    int n, n2, n3, n4;
    for (n=1;n > 0 || n < 11;n++)
    {
        n2 = pow(n, 2);
        n3 = pow(n, 3);
        n4 = pow(n, 4);
    }
    system("PAUSE");
    return 0;         
}

I keep getting errors for pow even though that's how I was taught to do it.. What am I doing wrong? And why does the C++ I'm getting taught at college seem completely different from the C++ everyone else is using.. What I mean is, why has it changed so much from 1996 (or whenever the Borland 5.01 compiler was available) to now?

Thanks

Edit: Taken some ints out of that code that I was messing about with.

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

try this

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <iostream>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{

int n2, n3, n4;

double n;

for (n=1;n > 0 || n < 11;n++)

{

n2 = pow(n, 2);

n3 = pow(n, 3);

n4 = pow(n, 4);

}

system("PAUSE");

cout << n2 << " " << n3 << " " << n4 << endl;

return 0;

}

remember to use the correct type for your variables i.e. n should be bouble or long int

hope it helps :)

EDIT: also, the if statement makes no sense and you will enter an infinite loop.

pls explain what you want the code to do so we can help more...

Link to comment
Share on other sites

  • 0

Thanks..

The question for the exercise was

Write a program to print out the values of N, N^2, N^3, N^4 for values of N = 1 to 10.

I only thought I needed to change that pow thing..

Link to comment
Share on other sites

  • 0

In that case you should only change the 'if' statement to read smt like this

for (n=1; n <= 10; n++)

The rest should be fine :)

Link to comment
Share on other sites

  • 0

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <iostream>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])

{

system("PAUSE");

for (int n=1;n <= 10;n++)

{

for (int x=1;x <= 10; x++)

{

cout << pow(n, x);

}

system("PAUSE");

return 0;

}

i am a bit unsure, and i havent tried it, but it should work, if the pow function is correct

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.