Question

In: Computer Science

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 + . . . + nˆ-x.
Use the iomanip library in order to print the sum with 50 decimal digits of precision.
Use the cmath library in order to access the pow function. For example pow(3,-x) returns the value
of 3ˆ-x.

Sample output:
Enter a value x = 5
Enter a non-negative integer (enter negative integer to quit): 4
1 + 2^-5 + 3^-5 + 4^-5 = 1.0363417888374486519609263268648646771907806396484
Enter a value x = -5

Enter a non-negative integer (enter negative integer to quit): 4
1 + 2^5 + 3^5 + 4^5 = 1300
Enter a value x = 6
Enter a non-negative integer (enter negative integer to quit): -2

C++.will like if correct,thank you.

Solutions

Expert Solution

Program :-

#include <iostream>
#include<iomanip>
#include<cmath>

using namespace std;

int main() {
int x, n, i;
float result = 0;
  
while(true)
{
cout << "Enter a value x = ";
cin >> x;
  
cout << "Enter a non-negative integer (enter negative integer to quit) : ";
cin >> n;
  
if(n<0)
break;
  
for(i=1; i<=n; i++)
{
result = result + pow(i, -x);
  
if(i==1)
cout << i ;
else
{
cout << " + " << i << "^" << -x;
}
}
cout << " = " << setprecision(50) << result;
cout << "\n";
result = 0;
}
  
cout << "-------------------EXIT-------------------";
cout << "\n";

return 0;
}


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 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! + ....
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
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...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT