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.
2 Réponses
+ 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;
}
}
0
tq so much naveen