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

1
2
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:
1
2
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.. :/
Then start with i=1 in the loop
and initialize t=1 before starting of the for loop
and have your sum+=t after the statement t*=x/i;
Now it should work f9..
Can you update the code then? Can you put it between the code tags too so its better to look at?
hmmm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream.h>
#include<conio.h>
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:"<<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;
t=1;
for(int i=1;i<=n;i++)
{
t*=(x/i);
sum+=t;
}
cout<<"Sum="<<sum;
getch();

}
Thanks alot! :)
You didn't listen to him...
Topic archived. No new replies allowed.