Counting numbers like 1, 2, 3, 4, 5, 6 … Basically, all integers greater than 0 are natural numbers.

Fact about Natural numbers
- They are whole numbers (called integers), and never less than zero (i.e. positive numbers)
- The next possible natural number can be found by adding 1 to the current natural number
- The natural numbers are the ordinary numbers, 1, 2, 3, etc., with which we count.
- The number zero is sometimes considered to be a natural number. Not always because no one counts starting with zero, 0, 1, 2, 3.
- GCD of all other natural numbers with a prime is always one.
- The natural numbers can be defined formally by relating them to sets. Then, zero is the number of elements in the empty set; 1 is the number of elements in the set containing one natural number; and so on.
How to print sum of n natural Numbers?
Using Recursion
Given a number n, find sum of first n natural numbers. To calculate the sum, we will use the recursive function recur_sum().
Examples :
Input : 3
Output : 6
Explanation : 1 + 2 + 3 = 6
Input : 5
Output : 15
Explanation : 1 + 2 + 3 + 4 + 5 = 15
C++
#include <iostream>
using namespace std;
int recurSum(int n)
{
if (n <= 1)
return n;
return n + recurSum(n - 1);
}
int main()
{
int n = 5;
cout << recurSum(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
public static int recurSum(int n)
{
if (n <= 1)
return n;
return n + recurSum(n - 1);
}
public static void main(String args[])
{
int n = 5;
System.out.println(recurSum(n));
}
}
|
Python
def recurSum(n):
if n <= 1:
return n
return n + recurSum(n - 1)
n = 5
print(recurSum(n))
|
C#
using System;
class GFG
{
public static int recurSum(int n)
{
if (n <= 1)
return n;
return n + recurSum(n - 1);
}
public static void Main()
{
int n = 5;
Console.WriteLine(recurSum(n));
}
}
|
PHP
<?php
function recurSum($n)
{
if ($n <= 1)
return $n;
return $n + recurSum($n - 1);
}
$n = 5;
echo(recurSum($n));
?>
|
Javascript
<script>
function recurSum(n)
{
if (n <= 1)
return n;
return n + recurSum(n - 1);
}
n = 5;
document.write(recurSum(n));
</script>
|
Output :
15
Time Complexity: O(n)
Auxiliary Space: O(n)
Using Loop
A simple solution is to do the following.
1) Initialize : sum = 0
2) Run a loop from x = 1 to n and
do following in loop.
sum = sum + x
C++
#include <iostream>
using namespace std;
int findSum(int n)
{
int sum = 0;
for (int x = 1; x <= n; x++)
sum = sum + x;
return sum;
}
int main()
{
int n = 5;
cout << findSum(n);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int findSum(int n)
{
int sum = 0;
for (int x = 1; x <= n; x++)
sum = sum + x;
return sum;
}
public static void main(String args[])
{
int n = 5;
System.out.println(findSum(n));
}
}
|
Python
def findSum(n) :
sum = 0
x = 1
while x <=n :
sum = sum + x
x = x + 1
return sum
n = 5
print findSum(n)
|
C#
using System;
class GFG{
static int findSum(int n)
{
int sum = 0;
for (int x = 1; x <= n; x++)
sum = sum + x;
return sum;
}
public static void Main()
{
int n = 5;
Console.Write(findSum(n));
}
}
|
PHP
<?php
function findSum($n)
{
$sum = 0;
for ($x = 1; $x <= $n; $x++)
$sum = $sum + $x;
return $sum;
}
$n = 5;
echo findSum($n);
?>
|
Javascript
<script>
function findSum(n)
{
let sum = 0;
for (x = 1; x <= n; x++)
sum = sum + x;
return sum;
}
n = 5;
document.write(findSum(n));
</script>
|
Output :
15
Time Complexity: O(n)
Auxiliary Space: O(1)
Using Sum of n terms formula
Formula for finding sum of n natural numbers is given by n*(n+1)/2 which implies if the formula is used the program returns output faster than it would take iterating over loop or recursion. Time complexity is O(1).
Referral Link: Program to find sum of n natural numbers
More problems related to Natural Number:
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
03 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...