Question

(5 Marks) 3 Write a C sentinel-controlled while loop that will compute the sum of integers entered by the user ending with 999. Include all declaration and initialization of variables. Also, output the sum.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

int main()
{
   // Declare the integer variables num and sum to hold the user input and total sum
   int num, sum;
   // Intialize the variables to zero
   sum = num = 0;
   do
   {
       // Add the number entered by user to the sum
       sum = sum + num;
       // Ask user for input
       cout<<"Input a number: ";
       //Read input number from user
       cin >> num;
   }
   // If the user inputs 999, then terminate the loop. Else add number to sum and read input again
   while(num != 999);
  
   // Display the sum of user inputs
   cout<<endl<<"Sum of integers : " <<sum <<endl;
}


root@Malware: /mnt/d/Programs # root@Malware : /mnt/d/Programs # root@Malware : /mnt/d/Programs# Input a number: 5 Input a nu

Note: As user input 999 is used as a terminating condition, it must not be added to the sum. But if you requirement needs 999 also to be added, then kindly refer below program. Ping me back for any more requirement or doubts. Thanks.

#include <iostream>
using namespace std;

int main()
{
   // Declare the integer variables num and sum to hold the user input and total sum
   int num, sum;
   // Intialize the variables to zero
   sum = num = 0;
   // If the user inputs 999, then terminate the loop.
   while(num != 999)
   {
       // Ask user for input
       cout<<"Input a number: ";
       //Read input number from user
       cin >> num;
       // First Add the number entered by user to the sum without checking for 999
       // if number is 999 then terminate after adding to sum
       sum = sum + num;
   }
  
   // Display the sum of user inputs
   cout<<endl<<"Sum of integers : " <<sum <<endl;
}

Add a comment
Know the answer?
Add Answer to:
(5 Marks) 3 Write a C sentinel-controlled while loop that will compute the sum of integers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers....

    (While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers. Your program ends with the input 0. Determine and display how many positive and negative values have been read, the maximum and minimum values, and the total and average of the input values (not counting the final 0).

  • 17. (2 points) C++ Using a while loop, write the code statements to sum 10 integers...

    17. (2 points) C++ Using a while loop, write the code statements to sum 10 integers entered by a user. Display the sum. Hint: Be sure to declare, initialize, and use appropriate variables to count the repetitions and accumulate the sum.

  • Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel –...

    Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel – sentinel is used to indicate the end of input and must not to be considered as the valid data for the computation). Perform the following operations for the integers entered by the user. Display the sum of the numbers less than 100 Display the smallest of the positive integers Display the largest of the negative integers Display how many integers are between 100 and...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Write a complete C++ program using a while loop to read a list of integers ending...

    Write a complete C++ program using a while loop to read a list of integers ending with the integer 999. Count and output the number of integers in the list that are greater than 500. If there is no integer in the list that is greater than 500. output (only once) the message that "No integer in the list is greater than 500". Do not use functions.

  • 1. Write a for loop that prints the sum of all positive even integers less than...

    1. Write a for loop that prints the sum of all positive even integers less than 200. Assume that variables i and sum are already declared. 2. Write a while loop that prints the sum of all positive odd integers less than 100. Assume that variables i and sum are already declared. 3. Write a do-while loop that prints the sum of all positive multiples of 3 less than or equal to 150. Assume that variables i and sum are...

  • Write pseudocode while loop to sum all the values between 2 integers (A & B, input...

    Write pseudocode while loop to sum all the values between 2 integers (A & B, input by the user), including A and B, and print the resulting sum. A must be less than B, otherwise print 0.

  • What is a "sentinel value"? What is the format for a while loop? What is the...

    What is a "sentinel value"? What is the format for a while loop? What is the format for a do-while loop? .Write some instructions which will read a series of numbers (type double) and add them to a total until the number -1 is entered The - 1 is not added to the total. Write some instructions which will read a series of numbers (type double) and add them to a total until the character control-d is entered. Write a...

  • C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an...

    C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an initial number, followed by that number of integers. Output those integers' sum. Repeat until the initial number is O or negative. Ex: If the user enters 3 96 1 0, the output is 16 Ex: If the user enters 396125 3 0, the output is 16 Hints: Use a while loop as an outer loop. Get the user's initial number of ints before the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT