C++/Series calculation
Expert: Prince M. Premnath - 3/13/2008
Questioni have to Write a program to evaluate the following function?
P = x + x2 / 2! + x3 / 3! + x4 / 4!+………………..
and second
Write a program to evaluate the following function?
X = 1 + (1/2)2 + (1/3)3 + (1/4)4 +…………………..
plz help me with explaining it.
Answer Hi Dear Krishna !
Here is the code what actually you are lookin for
Code 1 : Programme to calculate the sequence P = x + x2 / 2! + x3 / 3! + x4 / 4!+………………..
#include <iostream.h>
#include <math.h>
double fact ( double n )
{
if( n == 0)
return 1;
else
return n * fact(n-1);
}
void main()
{
int n;
double x,p=0;
cin>>n>>x;
for(double i=1;i<=n;i++)
p += pow(x,i)/fact(i); // sum up all the terms 1..n
cout<<"Result " <<res;
}
Code 2 :
Programme to calculate the following sequence !
X = 1 + (1/2)2 + (1/3)3 + (1/4)4 +…………………..
/*
#include <iostream.h>
#include <math.h>
void main()
{
int n;
double res;
cin>>n;
for(double i=1;i<=n;i++)
res += pow((1/i),i);
cout<<"Result : " <<res;
}
*/
A careful walk through over this code will help you out to understand the logic !
Thanks and Regards !
Prince M. Premnath