how to write the program

Write a program to find the sum of series:
1+ x1/2! + X2/3!+ ……+Xn/(n+1)! ,where n is any number inputted by the user.
how to do your homework the right way?
1) what about X?
2) very similar issue has been discussed here: http://cplusplus.com/forum/beginner/13130/
3) at least show us what you already did or what's your problem with it
Template Metaprogramming is a perfect solution for this.
It isn't, since X is not known at compile time.
I agree with jsmith. X is unknown. So template metaprogramming would not work.
X is also inputted by the user.
Try this one then:
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
26
27
28
29
30
31
#include <iostream>
#include <cmath>                       //for pow(double a,double b)
using namespace std;

int main()
{
	int n;
	double x;
//we initialize the result with "1", so we will deal only with computing the n number of fractions
//in fact, we can change this line to 'result = 0', all we will have to do is change the parameters 
//inside the loop, since 1 = x0/1!
	double result = 1;
//I don't know if you already got this in school, but i assume you have some idea about factorials
//and way to compute it
	double factorial = 1;

	cout<<"Insert X: ";
	cin>>x;
	cout<<"Insert n: ";
	cin>>n;

	for(int i = 1; i <= n; i++)
	{
		factorial *=(i+1);    //factorial = (i+1)*factorial; i+1, since we start from 2! 
		result += pow(x,i)/factorial;
	}

	cout<<"Result is: "<<result;
	
	return 0;
}


hope this helps, you can also make this, by using recursion - I leave it to you as an extra HW
Last edited on
Thank you so much johnkravetzki (77).
i appreciate your help.
thanx once gain
Topic archived. No new replies allowed.