Sum In Range | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Sum In Range

Hi, I need help with this problem. You need to create a function that takes two parameters and returns the sum of all numbers between the two parameters inclusive. The given code takes two numbers from input and passes them to a function called rangeSum(). Explanation: For inputs 4 and 9, the returned value should be 39. Use the for loop inside the function to execute the required calculation. I would like to thank you for your time and help.

12th May 2022, 12:55 AM
Heaven Post
Heaven Post - avatar
1 Answer
0
This is my code and I am stuck. I'm unsure what to change. Thank you for helping me. input <- readLines('stdin') x <- as.integer(input[1]) y <- as.integer(input[2]) #define the function result<-0 rangeSum <-function(x,y) for(a in x:y){ result<-result+a print(sum(result)) } rangeSum (x, y)
14th May 2022, 2:51 AM
Heaven Post
Heaven Post - avatar