C++ Program to print sum of n natural numbers using do while
Program
#include<iostream>
using namespace std;
int main()
{
int n, count, sum;
sum = 0;
count = 1;
cout << "Enter the value of n\t";
cin >> n;
do
{
sum += count;
count++;
} while(count <= n);
cout << "Sum of first " << n << " natural numbers is: "<< sum << endl;
return 0;
}
Output
$ g++ sum-of-n-natural-numbers-using-do-while.cpp
$ ./a.out
Enter the value of n 80
Sum of first 80 natural numbers is: 3240