0
Any help please
Write a C++ program that keeps reading integers until 0 is entered. Then, it should output the number of positive values, the number of even values, the sum of positive values, and the sum of negative values.
7 Respuestas
+ 1
You can get the first input before the loop, and then subsequent inputs at the end of the while loop instead of at the start, i.e.
input
while integer != 0:
// conditionals
input
0
Post your code here
0
Use a loop to repeatedly read an integer until 0 is encountered. Use variables initialized to zero to keep track of the values you have to compute.
Inside the loop, use conditionals to increment the correct counters. A number is positive if it is > 0, in which case you can increment the positive number counter and add it to the sum of positive numbers. Otherwise, you can add it to the sum of negative values.
Furthermore, a number is even if the remainder of the number and two is zero, in which case you increment the even number counter.
Note positivity and evenness are not mutually exclusive, so you will need two different conditionals for them.
If you need further help, please post your code, along with an explanation where you are stuck/ where the code seems to fail.
0
#include <iostream>
using namespace std; {
int integer,posnbr=0,even=0,sumpos=0,sumneg=0;
do{
cout<<"Give integers \n";
cin>>integer;
if(integer>=0){
posnbr++;
sumpos=sumpos+integer;
}
else{
sumneg=sumneg+integer;
}
if(integer%2==0){
even++;
}
}while(integer!=0);
cout<<"Number of positive values: "<<posnbr<<endl;
cout<<"Number of even values: "<<even<<endl;
cout<<"sum of positive values: "<<sumpos<<endl;
cout<<"sum of negative values: "<<sumneg<<endl;
return 0;
}
0
This is the code I'm using but i need to change the do while loop
What should I do
0
What do you mean by "I need to change the do while loop"? Change to a different loop structure, i.e. a while loop?
0
Yes Shadow