Adding POW?

I'm trying to calculate the square of an odd number using POW, but for some reason it wont work? Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

using namespace std;

// declare a constant for upper range limit
// call it TEN_K and set it to 10000 
const int TEN_K = 10000;

int main()
{
    for(int number = 0 ; number <TEN_K ; number++ )
       if (number % 2 == 0) //numberIsEven
       {
           return 0;
       }
       if (number % 2 != 0) //numberIsOdd
       {
           int square;
           square = pow(number);
       }
    return 0;
}
pow takes two arguments, the number and the exponent. So you would want to say pow(number, 2). However it's overkill to use pow to square a number when you can simply say number * number.
closed account (E0p9LyTq)
Have you read the documentation for std::pow()? The function takes TWO parameters, not one.
http://www.cplusplus.com/reference/cmath/pow/?kw=pow

The first time through your loop the program will exit (return 0). 0 % 2 == 0.

The if statement to check if the number is odd will be done after the loop ends, even if you didn't return in the if block for even.

Even after adding the 2nd parameter it's still not working?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

using namespace std;

// declare a constant for upper range limit
// call it TEN_K and set it to 10000 
const int TEN_K = 10000;

int main()
{
    for(int number = 0 ; number <TEN_K ; number++ )
       if (number % 2 == 0) //numberIsEven
       {
           return 0;
       }
       if (number % 2 != 0) //numberIsOdd
       {
           float square;
           square = pow(number, 2);
       }
    return 0;
}
closed account (E0p9LyTq)
You have no output. No way to tell if it works or not.

Your for loop will be executed one time. You check if number is even, 0 is an even number, and you exit the program by returning.

Even if you were not exiting your if statement to check for number being odd is after the for loop ends.

No output, no matter how the program ends you see nothing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>

// declare a constant for upper range limit
// call it TEN_K and set it to 10000 
const int TEN_K = 100;

int main()
{
   for (int number = 0; number < TEN_K; number++)
   {
      if (number % 2 == 0) //numberIsEven
      {
         // return 0;
      }

      if (number % 2 != 0) //numberIsOdd
      {
         std::cout << number << ' ';
         int square = std::pow(number, 2.0);
         std::cout << square << '\t';
      }
   }
}

1 1     3 9     5 25    7 49    9 81    11 121  13 169  15 225  17 289  19 361
21 441  23 529  25 625  27 729  29 841  31 961  33 1089 35 1225 37 1369 39 1521
41 1681 43 1849 45 2025 47 2209 49 2401 51 2601 53 2809 55 3025 57 3249 59 3481
61 3721 63 3969 65 4225 67 4489 69 4761 71 5041 73 5329 75 5625 77 5929 79 6241
81 6561 83 6889 85 7225 87 7569 89 7921 91 8281 93 8649 95 9025 97 9409 99 9801
Thank you, FurryGuy! I was thinking something similar but I wasn't sure!
closed account (E0p9LyTq)
If you are not going to do anything with an even number you could remove the even if statement block.

Just check for odd.

As tpb said, why use a sledgehammer to kill a fly by using std::pow().
even pow is not awesome for small integer powers. use x*x for squares... multiplication is much cheaper.

...
for just an output spew of them, its a one liner..

for(int i = 1; i < tenk; i+=2, cout << i*i<<endl);
Last edited on
Topic archived. No new replies allowed.