algorithm to find sum 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: Determine the sum of al digits of n
def sum_of_digits(n):
sum = 0
while (n != 0):
sum = sum + int(n % 10)
n = int(n/10)
return sum