by CodeChum Admin
I always want to look at the positive side of things, so I decide to seriously look at positive numbers, too!
Will you code along with me?
Instructions:
Input
Multiple lines containing an integer on each.
2
3
4
-1
-5
1
0
Output
A line containing an integer.
10
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
int main() {
int sum=0, n;
srand(time(NULL));
n = rand()%20-10; // Random int
cout<<n<<endl;
if(n>0)
sum=n;
while(n!=0) {
n = rand()%20-10; // Random int
if(n>0)
sum=sum+n;
cout<<n<<endl;
}
cout<<endl << sum<<endl;
return 0;
}
Comments
Leave a comment