C++ tutorial-loops: sum input values


Loops-sum input values

Let see other C++ code examples of using loops--for loop, while loop, and do while loop to enable a user to input a number of data points and then calculate the total of the data values. We provide three separate C++ code examples to solve the same problem. One C++ code example is using for loop and the two others are using while loop and do while loop.

Example 1: C++ code using for loop to sum input values

#include <cstdlib>
#include <iostream>
#include <conio.h>


using namespace std;

void sumValues(){
  int sum=0,val,n,i;
  cout<<"Enter number of data points:";
  cin>>n;
  for(i=0;i<n;i++){
     cout<<"Values "<<i<<":";
     cin>>val;
     sum+=val;
     cout<<"\n";
    }

  cout<<"Total:"<<sum;
}

//Start main
int main(){

   sumValues();
   getch();
   return 0;

}

//End main

Example 2: C++ code using while loop to sum input values

#include <cstdlib>
#include <iostream>
#include <conio.h>

using namespace std;

void sumValues(){
  int sum=0,val,n,i=0;
  cout<<"Enter number of data points:";
  cin>>n;
  while(i<n){
    cout<<"Values "<<i<<":";
    cin>>val;
    sum+=val;
    i+=1;
    cout<<"\n";
  }

  cout<<"Total:"<<sum;
}

//Start main
int main(){
  sumValues();
  getch();
  return 0;

}

//End main

Example 3: C++ code using do while loop to sum input values

#include <cstdlib>
#include <iostream>
#include <conio.h>


using namespace std;

void sumValues(){
  int sum=0,val,n,i=0;

  cout<<"Enter number of data points:";
  cin>>n;
  do{
     cout<<"Values "<<i<<":";
     cin>>val;
     sum+=val;
     i+=1;
     cout<<"\n";
  }while(i<n);

  cout<<"Total:"<<sum;
 }

//Start main
int main(){

  sumValues();
  getch();
  return 0;

}

//End main


Comments

Troy comment

 Troy

for more C++, C exercises,
please visit http://ccphplusplus.blogspot.com/


2015-05-26
Dara comment

 Dara

You can try the following code:
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
int num;
cout<<"Enter a number:";
cin>>num;
int s=0;
int temp=num;
while(temp>0){
int re=temp%10;
s+=re*re*re;
temp/=10;

}
if(s==num) cout<<"It is an Armstrong number.\n";
else cout<<"It is not an Armstrong number.\n";
system("PAUSE");
return EXIT_SUCCESS;
}


2013-12-24
Yatoni comment

 Yatoni

Anybody can help me? I want to create a C++ program to allow the user to input a number. Then the program tells he/she whether the number is Armstrong or not. Thanks


2013-12-24



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.