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
Write a program that performs the following two tasks: Reads an arithmetic expression in an infix...
Write a program that performs the following two tasks: 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 the algorithms described in class/ lecture notes. Use linked lists to implement the Queue and Stack ADTs. Using Java built-in classes will result in 0 points. You must use your own Stack and Queue classes (my code is a...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
For your first project, write a C program (not a C++ program!)that will read in a...
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer. Example:List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! your C program will contain the following: •Write a function that will make a copy of the values from one array to another array....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT