Are you searching for how to find the sum of natural numbers in R? Use the following R code to find the sum of natural numbers.
1 2 3 4 5 6 7 8 | # Program to find the sum of natural numbers upto n using recursion calculate_sum() <- function(n) { if(n <= 1) { return(n) } else { return(n + calculate_sum(n-1)) } } |
Program Output
1 2 | > calculate_sum(7) [1] 28 |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.