Question

In: Computer Science

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 the iomanip library in order to print the sum with 50 decimal digits of precision.

Sample output:

Enter a rate r = 4
Enter a non-negative integer (enter negative integer to quit): 9
1 + 4^1 + 4^2 + 4^3 + 4^4 + 4^5 + 4^6 + 4^7 + 4^8 + 4^9 = 349525
Enter a rate r = 0
Enter a non-negative integer (enter negative integer to quit): 3
1 + 0^1 + 0^2 + 0^3 = 1
Enter a rate r = -4
Enter a non-negative integer (enter negative integer to quit): -2

C++

Will like if correct,thank you.

Solutions

Expert Solution

CODE -

#include<iostream>

#include<cmath>

#include<iomanip>

using namespace std;

int main()

{

    int r;

    int n;

    int sum;

    cout << setprecision(50);

    while(true)

    {

        // Take rate as input from the user

        cout << "Enter a rate r = ";

        cin >> r;

        // Take a non-negative integer as input from the user

        cout << "Enter a non-negative integer (enter negative integer to quit): ";

        cin >> n;

        // Exit the loop if user enters a negative integer

        if(n<0)

            break;

        // Initialize the sum to 1

        sum = 1;

        cout << sum;

        // Loop to compute partial sum for the geometric series and display the equation

        for(int i=1; i<=n; i++)

        {

            sum += pow(r, i);

            cout << " + " << r << "^" << i;

        }

        // Display the sum

        cout << " = " << sum << endl;

    }

}

SCREENSHOTS -

CODE -

OUTPUT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

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 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! + ....
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
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;...
ARDUINO Create a function that will ask the user to enter the values for each of...
ARDUINO Create a function that will ask the user to enter the values for each of the three colors (red, green and blue) between 0 and 255. The function should check that the user does not enter a number bigger than 255 or smaller than 0. If this happens, the function should keep asking for a valid number.
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...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. 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...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
in java Write a while loop to ask the user to type number of hours(double) they...
in java Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than or equal to 0 and less than 5, then:  salary = numberofhours * 5, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT