EBK C++ PROGRAMMING: FROM PROBLEM ANALY
8th Edition
ISBN: 8220103648530
Author: Malik
Publisher: Cengage Learning US
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 5, Problem 40SA
Explanation of Solution
The output of the above segment using for loop is “value=479”.
Below is the code converted into while loop with same output “value=479”.
#include <iostream>
using namespace std;
int main()
{int i=0; //initialize the value of i here
int value = 3;
while(i<5) //while loop replacing for loop
{
value = value * (i + 1) + i;
i++; // increment the value of i inside the loop
}
cout << “value = “ << value << endl;
return 0;
}Below is the code converted into do while loop with same output “value=479”...
Expert Solution & Answer
Trending nowThis is a popular solution!
Students have asked these similar questions
JAVA CODE PLEASE
Functions With No Parameters and Return Values Quiz
by CodeChum Admin
Create a function named banner that prints “CodyChum” with a newline.
In the main function, write a program that accepts an integer input which would serve as an inclusive stopping point of a loop iteration that starts at the value of 1.
If the number that loops through the range is divisible by either 2 or 3, then call the banner function, otherwise just print the number.
Input
1. One line containing an integer
Output
First line is the integer user input.
Succeeding multiple lines containing an integer or “CodyChum”
10
1
CodyChum
CodyChum
CodyChum
5
CodyChum
7
CodyChum
CodyChum
CodyChum
#include <iostream>using namespace std;int main(){int i = 1;i = i - 1;while (i){cout << "its a while loop";i++;}return 0;}
Write code that uses any type of loop. The code should continually ask for a user input and sums all user inputs that are
divisible by 2. The loop should continue until the user enters a negative number. The code should output the sum a single
time once user entry has completed.
You may assume all libraries and namespaces have been previously written into the code, you are just writing everything
that would go inside the main function (beyond the return 0:).
Chapter 5 Solutions
EBK C++ PROGRAMMING: FROM PROBLEM ANALY
Ch. 5 - Prob. 1TFCh. 5 - What type of loop, such as counter control or...Ch. 5 - Prob. 20SACh. 5 - Prob. 27SACh. 5 - Prob. 40SACh. 5 - Prob. 45SACh. 5 - Write a program that prompts the user to input an...Ch. 5 - Write a program that prompts the user to input a...Ch. 5 - Write a program that uses while loops to perform...Ch. 5 - Redo Programming Exercise 8 using dowhile loops.
Knowledge Booster
Similar questions
- (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by initializing 10 counters to 0, and then generate a large number of pseudorandom integers between 0 and 9. Each time a 0 occurs, increment the variable you have designated as the zero counter; when a 1 occurs, increment the counter variable that’s keeping count of the 1s that occur; and so on. Finally, display the number of 0s, 1s, 2s, and so on that occurred and the percentage of the time they occurred.arrow_forwardProgram C Loop, pls help ASAP int getNum(); /*accepts input from the user.*/ void display(int num); /*prints the number pattern*/ ---------------------------------------------------------------------arrow_forwardComputer Sciencearrow_forward
- 2- The factorial n! of a positive integer n is defined as n! = 1*2*3 . .. * (n-1) * n Where 0! = 1 Write a function to calculate the factorial of a number. Argument: A number n of type unsigned int. Returns: The factorial n! of type long double. Write two versions of the function, where the factorial is • calculated using a loop calculated recursively Test both functions by outputting the factorials of the numbers 0 to 20.arrow_forwardThe purpose of each function should be documented? The purpose of each while-loop should be documented? The program runs def main(): totalBottles = 0 counter = 1 todayBottles = 0 totalPayout = 0 keepGoing = "y" while keepGoing.lower() == "y": totalBottles = getBottles() totalPayout = calcPayout(totalBottles) printInfo(totalBottles, totalPayout) print("Do you want to enter another week’s worth of data?") print("(Enter y or n)") keepGoing = input() def getBottles(): NBR_OF_DAYS = 7 totalBottles = 0 todayBottles = 0 counter = 1 while counter <= NBR_OF_DAYS: print(f"Enter number of bottles returned for day #{counter}:") todayBottles = int(input()) totalBottles += todayBottles counter += 1 return totalBottles def calcPayout(totalBottles): PAYOUT_PER_BOTTLE = .10 totalPayout = totalBottles * PAYOUT_PER_BOTTLE return totalPayout def printInfo(totalBottles, totalPayout):…arrow_forwardint calculatepower (int x, int y){ if (y > 0) return x* else return 1; int main (){ int num, pwr; cout > num; cout > pwr; cout <« "Number "<arrow_forwardCENGAGE MINDTAP 4-Accumulating Totals in a Loop in Java Accumulating Totals in a Loop f Ⓡ Summary In this lab, you add a loop and the statements that make up the loop body to a Java program that is provided. When completed, the program should calculate two totals: the number of left-handed people and the number of right-handed people in your class. Your loop should execute until the user enters the character X instead of L for left-handed or R for right-handed. The inputs for this program are as follows: R, R, R, L.L.L. R. L, R, R, L, X Variables have been declared for you, and the input and output statements have been written. Instructions 1. Ensure the file named LeftOrRight.java is open. 2. Write a loop and a loop body that allows you to calculate a total of left-handed and right-handed people in your class. 3. Execute the program by clicking Run and using the data listed above and verify that the output is correct. Grading When you have completed your program, click the Submit…arrow_forwardJAVA CODE ONLY AND PROVIDE OUTPUT SCREENSHOT PLEASEarrow_forwardExamine the following while loops and determine the value of ires at the end of each of the loops, and the number of times each loop executes. (a) ires = 1; while mod (ires, 15) = 0 ires = ires + 1; end ires = 15 number of times 16 (b) ires = 5; while ires 100 ires = ires^2; end ires = number of timesarrow_forwardcalcAverage function: This is function that returns no value and accept no parameters. This function will do the following: Ask the user to enter how many integers to enter. This has to be a positive integer and you need to validate that Write a “for” loop to accept this many integers and write the needed logic to calculate the average of these numbers and print that out Then the function will return back to the main functionarrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_iosRecommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning