5.7

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Write a statement that increments the value of the int variable total by the value of the int variable amount. That is, add the value of amount to total and assign the result to total.

total+=amount;

Given that two int variables , total and amount, have been declared , write a loop that reads integers into amount and adds all the non-negative values into total. The loop terminates when a value less than 0 is read into amount. Don't forget to initialize total to 0.

total=0; do { cin >> amount; if (amount>0) total += amount; } while (amount >=0);

Given that two int variables , total and amount, have been declared , write a sequence of statements that:initializes total to 0reads three values into amount, one at a time.After each value is read in to amount, it is added to the value in total (that is, total is incremented by the value in amount).

total=0; for (int i=1; i<=3; i++) { cin >> amount; total += amount; }

Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared , use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 111 + 222 + 333 + 444 into total. Use no variables other than n, k, and total.

total=0; for (int k=1; k<=n; k++) { total += k*k*k; }


Ensembles d'études connexes

Network & Internet Tech - Final Exam

View Set

Ch. 8-14 Quizzes (Exam 3) [Mgmt]

View Set

ASTB ANIT/mechanical Study Guide

View Set