You are here:

C++/Series calculation

Advertisement


Question
i 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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2016 About.com. All rights reserved.