Find the sum of natural numbers with and without recursion | faceprep

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

#include <stdio.h>
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;
}

#include <iostream>
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 << ” Natural Numbers is ” << sum_of_natural_numbers(n);
cout << endl;
return 0;
}

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));
}
}

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))

Time Complexity: O(n)

Program to find the sum of natural numbers using recursion

// sum of natural numbers using recursion in c

#include <stdio.h>
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++ program to find sum of natural numbers using recursion

#include <iostream>
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 << ” Natural Numbers is ” << sum_of_natural_numbers(n);
cout << endl;
return 0;
}

// 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));
}
}

# 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))

Time Complexity: O(n)

sum of natural numbers