Dear sir ,
I want to make a program to calculate
sum=1^3-2^3+3^3-4^3........+n^3
I have written the following code
#include<iostream>
#include<conio>
int main()
{
int sum=0,i,b,n;
std::cout<<"\n enter a value \n";
std::cin>>n;
for(i=1;i<=n;i++)
{
b=(-1)^(i+1);
sum=sum+b*i*i*i;
}
std::cout<<"\n the sum is \n"<<sum;
getch();
return 0;
}
But I am not getting correct output.
I would be glad you can solve my problem
With regards,
Matanuragi
you have correctly indicated that the difference in each term is in the value of the coefficient and its sign.
What you need to do is write a single equation that represents a single term, then turn that equation into code. This is what belongs in the loop (as I think you know).
Here's the equation for you:
n
Σ (-1)k+1(k3) k=1
Now make sure your code matches that math and let us know if you have any troubles.
Good luck!
[edit] Fixed the power of -1 to k+1. Sorry about that!
Dear sir,
As per my knowlege in maths the equation of kth term given by you i.e.
n
Σ (-1)k(k3)
k=1
gives the following series
-13 + 23 - 33 + 43 ...
According to me to calculate the series
13 - 23 + 33 - 43 ...
the kth term is
n
Σ (-1)k+1 k[sup]3
k=1
If I am still wrong ,Kindly forgive me for my foolishness and
explain explicitly the reason for me being wrong.
With regards,
Matanuragi
It is alright enfact I am thankful that you devoted your time in solving my problem.But I am still getting error that pow ()function is not in math header file in the following program
int main()
{
int sum=0,i,n,b;
std::cout<<"\n enter a value \n";
std::cin>>n;
for(i=1;i<=n;i++)
{
b=pow(-1,i+1);
sum=sum+b*i*i*i;
}
std::cout<<"\n the sum is \n"<<sum;
getch();
return 0;
}
Please rectify the error
With regards,
Matanuragi