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

The idea of the following sequence is to create a C++ function which will calculate the sum of all the  positive numbers from 0 to a user defined number (n).

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

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

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

Leave a Reply

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