summing odd cubed numbers and printing them

Hi, i cant figure out whats wrong with my code but i need to see the total sum of the odd integers cubed from number say 1 till x
#include <iostream>
#include <cmath>

using namespace std;
int main()
{

int x;
cout << " enter the value for n: " << endl;
cin >> x;
int i = 1;
int sum = 0;
while (i <= x)
{
if (!(i %2==0))
{
pow(i,3);
sum += i;
}
i++;

}
cout << "The sum of odd integers cubes from 1 " << " to " << x<< "is = "<< sum<< endl;


return 0;
}

Last edited on
First, posting code in code tags, <>, makes it more readable. See http://www.cplusplus.com/articles/jEywvCM9/

Second, have you studied http://www.cplusplus.com/reference/cmath/pow/


PS. The pow() may seem fancy, but how about plain i*i*i?
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
   unsigned N;
   cout << "Input N: ";   cin >> N;
   N += N % 2;
   cout << "Sum of odd cubes is " << N * N * ( N * N - 2 ) / 8;
}
Topic archived. No new replies allowed.