Connecting Tech Pros Worldwide Forums | Help | Site Map

TO print sum of series of numbers

Newbie
 
Join Date: Sep 2009
Posts: 1
#1: Sep 27 '09
I want to write a c program to print sum of series of numbers. Can anyone please send me the program code.

Needs Regular Fix
 
Join Date: Jul 2008
Posts: 386
#2: Sep 27 '09

re: TO print sum of series of numbers


If I send you the code, how would you be able to write it on your own when you already have it?
Familiar Sight
 
Join Date: Jan 2007
Posts: 193
#3: Sep 30 '09

re: TO print sum of series of numbers


One way to do this is to use a while loop and load an int or double type vector with the numbers you want to sum. Then use a for loop to to get each number from the vector and make a running sum.
e.g. this snippet explains.
Expand|Select|Wrap|Line Numbers
  1. vector<int>v;   //declare vector
  2. while(cin>>n)             //press any key which is not an int to cease input (say ~)
  3.    v.push_back(n);      //load vector elements
  4. for(int i=0;i<v.size();++i)
  5.    sum+=v[i];             //develop sum
  6. cout<<"sum of numbers"<<sum<<endl;
Reply