1# Sum of natural numbers up to num
2
3num = 16
4
5if num < 0:
6 print("Enter a positive number")
7else:
8 sum = 0
9 # use while loop to iterate until zero
10 while(num > 0):
11 sum += num
12 num -= 1
13 print("The sum is", sum)
14
1#include<bits/stdc++.h>
2using namespace std;
3int main()
4{
5 int n;
6 int sum=0;
7 cin>>n;
8 for(int i=1;i<=n;i++)
9 {
10 sum+=i;
11 }
12 cout<<sum<<" ";
13 cout<<endl;
14 return 0;
15}
1Sum of the First n Natural Numbers. We prove the formula 1+ 2+ ... + n = n(n+1) / 2, for n a natural number
1int addNumbers(int n)
2{
3 int count, sum = 0;
4
5 for(count=1; count <= n; count++)
6 {
7 sum = sum + count;
8 }
9
10 return sum;
11}