SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
More Flow Of Control
Exercise:
Programming Projects
Question:11 | ISBN:9780321531346 | Edition: 7

Question

The value ex can be approximated by the sum 1 + x + x2/2! + x3/3! + ... + x n/n!

Write a program that takes a value x as input and outputs this sum for n taken to be each of the values 1 to 100. The program should also output ex calculated using the predefined function exp. The function exp is a predefined function such that exp(x) returns an approximation to the value ex. The function exp is in the library with the header file cmath. Your program should repeat the calculation for new values of x until the user says she or he is through.

Use variables of type double to store the factorials or you are likely to produce integer overflow (or arrange your calculation to avoid any direct calculation of factorials). 100 lines of output might not fit comfortably on your screen. Output the 100 output values in a format that will fit all 100 values on the screen. For example, you might output 10 lines with 10 values on each line.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	double x;
	double add;
	double fact;	
	char choice;
	do
	{
		cout << "Enter value of x: ";
		cin >> x;
		cout << "\nThe value of exp(" << x << "): " 
<< exp(x) << endl;
		cout << "The value of e^x for n = 1 to 100 
using the equation: " << endl;
		add = 1; 
		for(int n = 1; n <= 100; n++)
		{
			fact = 1;			
			for(int m = n; m > 0; m--)
				fact = fact * m;
			add = add + pow(x, n) / fact;
			if(n % 10 == 1)				
				cout << endl;
			cout << add << " ";	
		} 
	cout << "\nDo you want to calculate again? (Y/N): ";
		cin >> choice;
		cout << endl;
	}while(choice == 'y' || choice == 'Y');
	return 0;
}

Output:

Enter value of x: 10

The value of exp(10): 22026.5
The value of e^x for n = 1 to 100 using the equation:

11 61 227.667 644.333 1477.67 2866.56 4850.68 7330.84 10086.6 12842.3
15347.5 17435.2 19041.1 20188.2 20952.9 21430.8 21712 21868.2 21950.4 21991.5
22011.1 22020 22023.8 22025.4 22026.1 22026.3 22026.4 22026.4 22026.5 22026.5
22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5

22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5

22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5

22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5

22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5

22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5

22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5 22026.5

Do you want to calculate again? (Y/N):n

 

0 0

Discussions

Post the discussion to improve the above solution.