Hey everyone, I am having troubles with some problems below, Ihave done my part
ID: 3611719 • Letter: H
Question
Hey everyone, I am having troubles with some problems below, Ihave done my part of work. But the results are not coming accurate.Can any of you please have a look at it? Here is what I have done so far: Question) 4) The factorial of a nonnegative integer n iswritten n! (pronounced "n factorial") andis defined as follows:n! = n * (n-1) * (n-2) * ... * 10! = 1 and 1! = 1 by definition.For example, 5! = 5*4*3*2*1 = 120a) Write a program that reads a nonnegative integer and computes and prints its factorial.Do not use scientific notation for displaying the result.b) Write a program that estimates the value of mathematical constant e by using the formula:e = 1 + (1/1!) + (1/2!) + (1/3!) ...Prompt the user for the desired accuracy of e, i.e. the number of terms in the summation.Use 10 digits of precision to display the result.Expected sample output:Number for Factorial : 1616! = 20922789888000.000000Desired Accuracy for "e"(number of terms in the series) : 10e with 10 terms = 2.7182815256 (with 10 digits of accuracy)
My Work:
#include <iostream>#include<iomanip>#include <cmath>using namespace std;int factorial(int factorialParameter){int answer = 1, current = 1;while (current <= factorialParameter) {answer *= current;current++;}return answer;}double math(int mathParameter){int cnt=1;double factorial=1.0, sum=0.0;while(cnt<=mathParameter){factorial=factorial*cnt;double term=cnt/factorial;sum=sum+term;cnt++;}return sum;}int main() {int n;int answer;double b;int d,places=0;cout << "Number for Factorial: ";cin >> n;answer=factorial(n);b=math(n);cout << n << "! = " << answer<<" ";cout<<"Desired Accuracy for e(number of terms in the series): ";cin>>d;while(places<=d){cout<<setprecision(places++);}cout<<"e with "<<d<<" decimals is "<<b<<" ";system("pause");return 0;}Due to some unknown reasons its not working properly! Please help me out. I always rate. Also, can anyone tell me how to put this formula?
e^x = 1 + ((x^1)/1!) + ((x^2)/2!) + ((x^3)/3!) ...Im struggling withthis. I am having troubles with some problems below, Ihave done my part of work. But the results are not coming accurate.Can any of you please have a look at it? Here is what I have done so far: Question) 4) The factorial of a nonnegative integer n iswritten n! (pronounced "n factorial") and Im struggling withthis.