summation of n natural numbers code example
Example 1: sum of n natural numbers
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
int sum=0;
cin>>n;
for(int i=1;i<=n;i++)
{
sum+=i;
}
cout<<sum<<" ";
cout<<endl;
return 0;
}
Example 2: formula for sum of n numbers
Sum of the First n Natural Numbers. We prove the formula 1+ 2+ ... + n = n(n+1) / 2, for n a natural number