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)
5 Réponses
+ 4
rangeSum <-function(x,y){
for(s in x:y){
r <-s+r
}
return (r)
}
print(rangeSum (x, y))
+ 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))
+ 3
Thank you guys
+ 2
rangeSum <-function(x,y){
return (sum(x:y))
}
print(rangeSum (x, y))
+ 1
Simba the same changes at the same time :D