Programming Examples
Cpp program to accept and print the sum of positive numbers only
Write a cpp program to find the sum of positive numbers only
If the user enters a negative number, the loop ends
and the negative number entered is not added to the sum
Solution
#include<iostream>
using namespace std;
int main(){
int num=0,sum=0;
do
{
sum+=num;
cout<<"Enter any number";
cin>>num;
}
while(num>=0);
cout<<"Sum is "<<sum;
return 0;
}
Output
Enter any number 12
Enter any number 8
Enter any number 0
Enter any number 2
Enter any number -4
Sum is 22