C++ Program to calculate the sum of n natural numbers using do while loop

Share on facebook
Share on linkedin
Share on twitter
Share on email
Use "ufd6qb86" For Offers


Code :

Result :

Program to calculate the sum of n natural numbers using do while loop
//Program to calculate the sum of n natural numbers using do while loop formula if you want sum = number * (1 + number)/2

#include<iostream>

using namespace std;

main()

{

 int number, sum = 0, i= 1;

 

 cout << "Enter the nth Number of Natural Number" << endl;

 cin >> number;

 

 do

 {    

     

  sum = sum + i; 

  i++; 

 }

 while(i <= number);

 

 cout << "the sum upto "<< number << "th Natural Number is : " << sum;

 

 return 0;

}
Get 20% Off on Trading Fee Use "LG3RJV7Z"

More To Explore

Use "ufd6qb86" For Offers