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();
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..:/
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..
#include<iostream.h>
#include<conio.h>
usingnamespace 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();
}