fastread homefastrread library fastread menu

R Programming : Find the Sum of Natural Numbers Using Recursion

Tutorial by:Maria Ghoste      Date: 2016-06-10 03:57:32

❰ Previous Next ❱

Here, we ask the user for a number and use recursive function recur_sum() to compute the sum upto that number.

Source Code

# Program to find the sum of
# natural numbers upto n
# using recursive function

recur_sum <- function(n) {
    if(n <= 1) {
        return(n)
    } else {
        return(n + recur_sum(n-1))
    }
}

Output


> recur_sum(7)
[1] 28

 

❰ Previous Next ❱


R Programming

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website