The sum of the first n integer numbers [using “for”]

We are going to use a C++ program sequence in order to calculate the sum of the first “n” given positive integer numbers by using “for”:


#include
void main()
{
int n, sum=0;
cout<<"Give the last number which will be added to the sum"; cin>>n;
for (int i=0; i<=n; i++)
sum=sum+i;
cout<<"The sum of all the positive integers between 0 and "<< n <<" is "<< sum;
}

Tagged on: , , , , ,

One thought on “The sum of the first n integer numbers [using “for”]

  1. Pingback: Retrospectiva săptămânii #1 : Cristi Juverdeanu

Leave a Reply

Your email address will not be published. Required fields are marked *