Question

In: Computer Science

You should write a small C++ program that performs the following tasks: First, your program should...

You should write a small C++ program that performs the following tasks:

  1. First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive.

  2. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to accept 5 new numbers from the user.

  3. Your program should allow the input of 5 numbers until the user enters 5 zero values. At this point, your program should then terminate.

Example input/output:

1 1 1 1 1

5

10 5 5 10 5

35

0 1 0 0 0

1

0 0 0 0 0

Solutions

Expert Solution

ANSWER:-

#include <iostream>
using namespace std;

int main() {
   int sum;
   int k=0,n;
   while(k!=5)
   {
       k=0;
       sum=0;
       cout<<"enter 5 number either 0 or positive : 10 5 5 10 5\n";
       for(int i=0;i<5;i++)
       {
           cin>>n;
           if(n==0)
           k++;
           sum=sum+n;
       }
       if(k!=5)
       cout<<sum<<endl;
   }
   return 0;
}

// OUTPUT:-

enter 5 number either 0 or positive : 1 1 1 1 

1

enter 5 number either 0 or positive : 10 5 5 10 5

35

enter 5 number either 0 or positive : 0 1 0 0 0

1

enter 5 number either 0 or positive : 0 0 0 0 0

// If any doubt please comment


Related Solutions

Write a program that performs the following two tasks in java Reads an arithmetic expression in...
Write a program that performs the following two tasks in java Reads an arithmetic expression in an infix form, stores it in a queue (infix queue) and converts it to a postfix form (saved in a postfix queue). Evaluates the postfix expression. Use linked lists to implement the Queue and Stack ADTs. DO NOT USE BUILT IN JAVA CLASSES
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
Your task is to write a C program which performs encryption and decryption of a message...
Your task is to write a C program which performs encryption and decryption of a message using the substitution cipher algorithm. Write a C program which performs encryption and decryption using the substitution cipher algorithm. Your program must be fully automated (ie: it does not take any interactive user input) and either encrypt or decrypt depending on the files which exist in the program’s directory. If message.txt exists your program should read that file, encrypt the contents, and write the...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
write a C program which performs encryption and decryption of a message
write a C program which performs encryption and decryption of a message
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2....
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2. The variable value1 has been initialized to 200000. Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Display the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Display the value of value2....
Write a C program that performs the following: Asks the user to enter his full name...
Write a C program that performs the following: Asks the user to enter his full name as one entry. Asks the user to enter his older brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have any older brother). Asks the user to enter his younger brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have...
Q1. Write an application that performs each of the following tasks: a) Specify that class “Pieceworker”...
Q1. Write an application that performs each of the following tasks: a) Specify that class “Pieceworker” inherits from class Employee. b) Create instance variables “Firstname” and “ID” in superclass and a toString to display. c) Add “branch” and toString in PieceWorker. d) Show overriding to display the variables of the subclass. Q2 Draw an inheritance hierarchy for students at a university. Use Student as the superclass of the hierarchy, and then extend Student with classes Undergraduate Student and Graduate Student....
Program Description Write and test a MASM program to perform the following tasks: Display your name...
Program Description Write and test a MASM program to perform the following tasks: Display your name and program title on the output screen. Display instructions for the user. Prompt the user to enter three numbers (A, B, C) in descending order. Calculate and display the sum and differences: (A+B, A-B, A+C, A-C, B+C, B-C, A+B+C). Display a closing message. Program Requirements The program must be fully documented and laid out according to the CS271 Style Guide. This includes a complete...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems. • stat.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the sum, average, sum of squares and population variance of the numbers. Remember, you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT