Answers for "sum of n natural numbers in python"

1

sum of n natural numbers in python

# Sum of natural numbers up to num

num = 16

if num < 0:
   print("Enter a positive number")
else:
   sum = 0
   # use while loop to iterate until zero
   while(num > 0):
       sum += num
       num -= 1
   print("The sum is", sum)
Posted by: Guest on January-18-2021
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;
}
Posted by: Guest on June-06-2021

Code answers related to "sum of n natural numbers in python"

Python Answers by Framework

Browse Popular Code Answers by Language