Program to find the sum of Natural Numbers using Recursion

0 Suresh Chand March 28, 2021

In this example, We will learn to find the sum of natural numbers using recursion.

To understand this examples, You must have knowledge in following topics.

#include<iostream>
using namespace std;

int add(int n);

int main() {
    int n;

    cout << "Enter a positive integer: ";
    cin >> n;

    cout << "Sum =  " << add(n);

    return 0;
}

int add(int n) {
    if(n != 0)
        return n + add(n - 1);
    return 0;
}

The output of above program is

Enter a positive integer: 5
Sum =  15
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

!

Ads

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System