0

I need to create a function that takes two parameters and returns the sum of all numbers between the two parameters inclusive.

This code prints the sum of all ranges, I want only one single sum value For example input( x= 2 , y= 4) range 2:4 then sum= 10 input <- readLines('stdin') x <- as.integer(input[1]) y <- as.integer(input[2]) #define the function result<-0 rangeSum <-function(x,y) for(s in x:y){ result<-s+result print (sum(result)) } rangeSum (x, y)

19th Sep 2021, 8:19 AM
Yuzer Zef
5 Answers
+ 4
rangeSum <-function(x,y){ for(s in x:y){ r <-s+r } return (r) } print(rangeSum (x, y))
19th Sep 2021, 8:31 AM
Simba
Simba - avatar
+ 4
input <- readLines('stdin') x <- as.integer(input[1]) y <- as.integer(input[2]) #define #the function r <- 0 rangeSum <-function(x,y){ for(s in x:y){ r <-s+r } return(r) } print(rangeSum (x, y))
19th Sep 2021, 8:31 AM
mesarthim
mesarthim - avatar
+ 3
Thank you guys
19th Sep 2021, 8:37 AM
Yuzer Zef
+ 2
rangeSum <-function(x,y){ return (sum(x:y)) } print(rangeSum (x, y))
20th Sep 2021, 5:47 AM
Cmurio
Cmurio - avatar
+ 1
Simba the same changes at the same time :D
19th Sep 2021, 8:32 AM
mesarthim
mesarthim - avatar