In: Computer Science
You should write a small C++ program that performs the following tasks:
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.
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.
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
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