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

By using a C++ program, we are going to determine the sum of the first “n” positive integers as we use the “do whil”e structure:


#include
void main()
{
int n, sum=0;
cout<<"Give the last number which will be added to the sum"; cin>>n;
do {
sum=sum+n;
n--;} while (n>0);
cout<<"The given sum is"<< sum;
}

 

Tagged on: , , , , ,

Leave a Reply

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