### Sum of series

Why isnt this prgram working? Need help fast! Its urgent!

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
float x, t, sum=0;
t=x;
cout<<"This program is for the sum of the following series:"<<endl;
cout<<"x+ (x^2/2!) + (x^3/3!) + (x^4/4!) + (x^5/5!)....(x^N/N!)"<<endl;
cout<<"Enter 'x' as per your choice:";
cin>>x;
cout<<"Enter 'n' as per your choice:";
cin>>n;
for(int i=2;i<=n;i++)
{
t*=(x/i);
sum+=t;
}
sum+=x;
cout<<"Sum="<<sum;
getch();

}
Last edited on
In the following code snip neither x nor t was initialized correctly. So they have undefined values

 ``12`` ``````float x, t, sum=0; t=x;``````
Last edited on
So what to do to correct it?
Need help really fast...this question ain't over yet! :/
You need to write it like so:
 ``12`` ``````cin>>x; t=x;``````
you can set t to x only when x has a valid value
initialise t=x after taking the input of x from the user.......
ok I did it, but even then the output comes out to be 8 when x is 4 and n is 2 but it should be 12...calculate pls... :/
Last edited on
okk you put you sum+=t statement b4 t*=x/i;
Can you clarify the maths on this? ` t*=(x/i);`

Is that a shortcut to raising x to the power of i, then dividing by i factorial?

Edit: Your for loop starts at `int i = 2;`
Last edited on
yelnatz..that is correct..its a shortcut for raising x to the power i and then dividing by i factorial.
ok suppose the user inputs x as 4 and n as 2 which means he requires the sum 4+(4^2/2!) which is equal to 12..but the program returns it as 8 thats my problem..and about the loop ..in it 'i'will have to start with 2 since i am considering (x^2/2!) + (x^3/3!) + (x^4/4!) + (x^5/5!)....(x^N/N!) series and then adding x to it in the last...so thats its..still the problem continues..:/
gaurya95..Its not giving the correct output even that way.. :/
 ``12345678910111213141516171819202122232425`` ``````#include #include using namespace std; void main() { clrscr(); int n; float x, t, sum=0; //t=x; cout<<"This program is for the sum of the following series:"<>x; cout<<"Enter 'n' as per your choice:"; cin>>n; t=1; for(int i=1;i<=n;i++) { t*=(x/i); sum+=t; } cout<<"Sum="<