Find the sum of natural numbers with and without recursion
Program to find the sum of natural numbers with and without recursion is discussed in this article. A number, N is obtained as input and the sum of first N natural numbers is given as output.
Program to find the sum of natural numbers without using recursion
C
#include
int sum_of_natural_numbers(int n)
{
int sum = 0;
for(int i = 1; i <= n; i++)
{
sum += i;
}
return sum;
}
int main()
{
int n;
printf(“\nEnter the number : “);
scanf(“%d”, &n);
printf(“\nSum of %d Natural Numbers is %d\n “,n,sum_of_natural_numbers(n));
return 0;
}
C++
#include
using namespace std;
int sum_of_natural_numbers(int n)
{
int sum = 0;
for(int i = 1; i <= n; i++)
{
sum += i;
}
return sum;
}
int main()
{
int n;
cout << “\nEnter the number : “;
cin >> n;
cout << “\nSum of ” << n>
cout << endl>
return 0;
}
JAVA
import java.util.*;
public class Main
{
static int sum_of_natural_numbers(int n)
{
int sum = 0;
int i;
for(i = 1; i <= n; i++)
{
sum += i;
}
return sum;
}
public static void main(String args[])
{
int n;
System.out.print(“\nEnter the number : “);
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println(“Sum of ” + n + “Natural Numbers is ” + sum_of_natural_numbers(n));
}
}
PYTHON 3
def sum_of_natural_numbers(n):
sum = 0
for i in range(1,n+1):
sum = sum + i
return sum
n = int(input(“Enter a number : “))
print(“Sum of”,n,”Natural Numbers is”,sum_of_natural_numbers(n))
Output
Input- Enter the number:6 Output- Sum of 6 Natural Numbers 21
Time Complexity: O(n)
Program to find the sum of natural numbers using recursion
C
// sum of natural numbers using recursion in c
#include
int sum_of_natural_numbers(int n)
{
if(n == 0)
return 0;
return n + sum_of_natural_numbers(n – 1);
}
int main()
{
int n;
printf(“\nEnter the number : “);
scanf(“%d”, &n);
printf(“\nSum of %d Natural Numbers is %d\n “,n,sum_of_natural_numbers(n));
return 0;
}
C++
// C++ program to find sum of natural numbers using recursion
#include
using namespace std;
int sum_of_natural_numbers(int n)
{
if(n == 0)
return 0;
return n + sum_of_natural_numbers(n – 1);
}
int main()
{
int n;
cout << “\nEnter the number : “;
cin >> n;
cout << “\nSum of ” << n>
cout << endl>
return 0;
}
JAVA
// sum of natural numbers usimg recursion java
import java.util.*;
public class Main
{
static int sum_of_natural_numbers(int n)
{
if(n == 0)
return 0;
return n + sum_of_natural_numbers(n – 1);
}
public static void main(String args[])
{
int n;
System.out.print(“\nEnter the number : “);
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println(“Sum of ” + n + “Natural Numbers is ” + sum_of_natural_numbers(n));
}
}
PYTHON 3
# Sum of natural numbers using recursion in python
def sum_of_natural_numbers(n):
if(n == 0):
return 0
return n + sum_of_natural_numbers(n – 1)
n = int(input(“Enter a number : “))
print(“Sum of”,n,”Natural Numbers is”,sum_of_natural_numbers(n))
Output
Input- Enter the number:6 Output- Sum of 6 Natural Numbers 21
Time Complexity: O(n)
Write a public review