C++ Program to Find Sum of First n Natural Numbers
Here you will get the source code to find the sum of first n natural numbers in C++ Programming.
Program code to Find Sum of First n Natural Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> using namespace std; int main() { int num,i,sum=0; cout<<"\nEnter a Number :"; cin>>num; i=1; while(i<=num) { sum=sum+i; i++; } cout<<"\nThe sum of first n natural numbers is = "<<sum; return 0; } |
Output
Enter a Number : 8
The sum of first n natural numbers is = 36
Check out our other C++ programming Examples