If you want to start learning the R programming or you have a bit knowledge about R programming, then this tutorial will help you to explore more. In this tutorial, we are going to share a R program to find the sum of natural numbers with an example.
Try more:
We assume you have the basic knowledge of R programming language. Copy the below code and execute it to see the program output.
1 2 3 4 5 6 7 8 9 10 11 12 | num = as.integer(readline(prompt = "Enter a number: ")) if(num < 0) { print("Enter a positive number") } else { sum = 0 # use while loop to iterate until zero while(num > 0) { sum = sum + num num = num - 1 } print(paste("The sum is", sum)) } |
Enter a number: 10
The sum is 55
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.