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

Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2024 Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz