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 +...
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
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;...
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...
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...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to see if the value is a number between 1 and 10 #if number is too high or too low, tell user, if they guessed it break out of loop Display "Welcome to my Guess the number program!" random mynumber count=1 while True try Display "Guess a number between 1 and 10"   Get guess while guess<1 or guess>10 Display "Guess a number between 1 and...
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
(8 marks) Write a program to ask user to enter an integer that represents the number...
Write a program to ask user to enter an integer that represents the number of elements, then generate an ArrayList containing elements which are all random integers in range [75, 144] , and finally display index and value of each element. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use ArrayList. Your program must use only printf(…) statements to adjust the alignment of your output. Your code must display the index in descending...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT