Write a program that asks you to type a number. After each entry, the application reports the cumulative sum of entries. The program terminates when you enter a zero.
C
C
#include<iostream>
using namespace std;
int takeinput()
{
int num;
cout<<“\n\nPlease enter the next number or Press 0 to exit: “;
while(!(cin>>num))
{
cin.clear();
while(cin.get()!=’\n’)
continue;
cout<<“Wrong Input..So Please enter a valid input”;
cout << “\nPlease enter the number: “;
}
return num;
}
int cumulativesum(int num,int sum)
{
sum+=num;
return sum;
}
int main()
{
int sum=0,num;
num=takeinput();
while(num!=0)
{
sum=cumulativesum(num,sum);
cout<<“\nCumulative Sum= “<<sum;
num=takeinput();
}
return 0;
}
Here takeinput(), the function takes the input and stores it to num.
A running total is the summation of a sequence of numbers which is updated each time a new number is added to the sequence, by adding the value of the new number to the previous running total.