How to write a for loop code that calculated the sum of the first and natural number. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to write a for loop code that calculated the sum of the first and natural number.

for example,if the number entered is 5, the loop will calculate 1+2+3+4+5=15.

1st Jan 2017, 2:57 PM
fatinfarhanah
2 Answers
+ 1
Here is a code segment which prints the sum of first n natural numbers. void main () { int sum=0; int n; cout <<"Enter the numbers of terms : "; cin>>n; for (int i=1;i <=n;i++) { sum+=i; } }
1st Jan 2017, 3:04 PM
Naveen Kumar
Naveen Kumar - avatar
0
tq so much naveen
1st Jan 2017, 3:10 PM
fatinfarhanah