Question

In: Computer Science

Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer...

Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer to
quit):”
and store this into an int named n.
If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise,
in the remaining part of the while loop, use a for loop to compute the sum of the inverse factorials
from 0 to n, that is sum = 1/0! + 1/1! + 1/2! + . . . + 1/n!
Use the iomanip library in order to print the sum with 50 decimal digits of precision.

Sample output:
Enter a non-negative integer (enter negative integer to quit): 8
1/0! + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! + 1/6! + 1/7! + 1/8! = 2.7182787698412700372330164100276306271553039550781
Enter a non-negative integer (enter negative integer to quit): 0
1/0! = 1
Enter a non-negative integer (enter negative integer to quit): -3

Will like if correct thank you

c++

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).

Images of the Code:
Note: If the below code is missing indentation please refer code Images

Typed Code:

// include required header
#include <iostream>
// including iomanip header
#include <iomanip>
// using std
using namespace std;

// A function named factorial to calculate factorial
// with input parameter x
int factorial(int x)
{
// If x is 0
if(x==0)
{
// returning 1
return 1;
}
else // otherwise
{
// calling factorial recursively
return x*factorial(x-1);
}
}

int main()
{
// A int variable named n to store user input
int n;
// a while true loop
while(true)
{
// Prompt for User input
cout << "Enter a non-negative integer (enter negative integer to quit): ";
// reading value into n
cin >> n;
// if n is negative (less than 0)
if(n<0)
{
// breaking the while loop
break;
}
else // otherwise
{
// A variable to store sum of the inverse factorials
double sum = 0;
// for loop to compute the sum of the inverse factorials
for (int i = 0; i <= n; i++)
{
// calling function factorial with parameter as i
// adding the returned value to sum
sum = sum + (1/double(factorial(i)));
}
  
// a for to print the factorial expression
for (int i = 0; i < n; i++)
{
// print each term
cout << "1/" << i << "! + ";
}
// printing last term and equal's to symbol
cout << "1/" << n << "! = ";
// using iomanip method set precision to 50 decimal
//printing the sum
cout <<setprecision(50) << sum << '\n';
}
}
}
//code ended here

Output:


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

Use a while(true) loop to ask the user the following 2 values “Enter a rate r...
Use a while(true) loop to ask the user the following 2 values “Enter a rate r =” “Enter a nonnegative integer (enter negative integer to quit):” If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the partial sum for the geometric series, namely 1 + r + rˆ2 + rˆ3 + . . . +rˆn. Use...
Use a while(true) loop to ask the user the following 2 values “Enter a value x...
Use a while(true) loop to ask the user the following 2 values “Enter a value x “ “Enter a nonnegative integer n (enter negative integer to quit): “ If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the partial sum for the Riemann zeta series for the geometric series, namely 1 + 2ˆ-x + 3ˆ-x +...
Write a while loop that will let the user enter a series of integer values and...
Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user backwards. do Loop...
Inside a do while loop ask a user to enter two numbers (use a single scanf...
Inside a do while loop ask a user to enter two numbers (use a single scanf function). Inside the same loop ask the user whether they wish to find the min or max of the values entered. If the user enters a 0 call the minFunction; if the user enters a 1 call the maxFunction; otherwise print Not valid input. Pass the two first numbers entered as parameters to the functions. Inside minFunction and maxFunction calculate the minimun and the...
Use a for loop to ask a user to enter the grades of 5 courses. The...
Use a for loop to ask a user to enter the grades of 5 courses. The user should enter character values, e.g., A. Calculate the GPA of the user Hint: Convert the character values entered to numerals, e.g., A to 4 c programming help me please
C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1 is entered, stop prompting for numbers and display the maximum number entered by the user. I am struggling to have my program print the math function. Here is what I have so far: import java.util.*; public class MaxSentinel { public static void main(String[] args) {    Scanner input = new Scanner(System.in); System.out.println("Please enter a value. Press -1 to stop prompt."); int number = 0;...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT