Program to Find the Sum of Natural Numbers using Recursion
- Write a program to Find the Sum of Natural Numbers using Recursion in C
- Write a program to Find the Sum of Natural Numbers using Recursion in C++
- Write a program to Find the Sum of Natural Numbers using Recursion in Python
- Write a program to Find the Sum of Natural Numbers using Recursion in PHP
- Write a program to Find the Sum of Natural Numbers using Recursion in Java
- Write a program to Find the Sum of Natural Numbers using Recursion in Java Script
- Write a program to Find the Sum of Natural Numbers using Recursion in C#
Explanation:
Logic to Find the Sum of Natural Numbers Using Recursion
To find the sum of the first n natural numbers using recursion, we use the following approach:
- Base Case:
- If the number n is 0, return 0, as the sum of the first 0 natural numbers is 0.
- Recursive Case:
- For any number n, the sum of the first n natural numbers can be found by adding n to the sum of the first n – 1 natural numbers. Thus, the recursive formula is: Sum(n) = n + Sum(n−1)
- Stopping Condition:
- The recursion stops when n becomes 0.
Program to Find the Sum of Natural Numbers using Recursion
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> int sum_of_natural(int n) { if (n == 0) return 0; return n + sum_of_natural(n - 1); } int main() { int n; printf("Enter a number: "); scanf("%d", &n); printf("Sum of first %d natural numbers is %d\n", n, sum_of_natural(n)); return 0; }
#include <iostream> using namespace std; int sum_of_natural(int n) { if (n == 0) return 0; return n + sum_of_natural(n - 1); } int main() { int n; cout << "Enter a number: "; cin >> n; cout << "Sum of first " << n << " natural numbers is " << sum_of_natural(n) << endl; return 0; }
def sum_of_natural(n): if n == 0: return 0 return n + sum_of_natural(n - 1) n = int(input("Enter a number: ")) print(f"Sum of first {n} natural numbers is {sum_of_natural(n)}")
<?php function sum_of_natural($n) { if ($n == 0) return 0; return $n + sum_of_natural($n - 1); } echo "Enter a number: "; $n = intval(trim(fgets(STDIN))); echo "Sum of first $n natural numbers is " . sum_of_natural($n) . "\n"; ?>
import java.util.Scanner; public class Main { public static int sumOfNatural(int n) { if (n == 0) return 0; return n + sumOfNatural(n - 1); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); int n = scanner.nextInt(); System.out.println("Sum of first " + n + " natural numbers is " + sumOfNatural(n)); } }
function sumOfNatural(n) { if (n === 0) return 0; return n + sumOfNatural(n - 1); } const n = parseInt(prompt("Enter a number:")); console.log(`Sum of first ${n} natural numbers is ${sumOfNatural(n)}`);
using System; class Program { static int SumOfNatural(int n) { if (n == 0) return 0; return n + SumOfNatural(n - 1); } static void Main() { Console.Write("Enter a number: "); int n = int.Parse(Console.ReadLine()); Console.WriteLine($"Sum of first {n} natural numbers is {SumOfNatural(n)}"); } }