# solution to 6.3 exercise

I don't think the answer to 6.3 is correct. The question was "Write a for loop that sums all the numbers from 1 to n". You would expect to have a single value as a sum and not multiple sums for each loop. The print() statement must be outside the loop. The correct answer is:

```
n <- 10 # n = 10 just as an example
sum <- 0 # we set sum to zero. This is the initial value before the loop begin.
for(i in 1:n){
sum <- sum + i
}
print(sum)
[1] 55
```

or you could code like this:

```
n <- 10
x <- vector(length=n)
for ( i in 1:n){
x[i] <-i
}
print(sum(x))
[1] 55
```

0 answers ( 0 marked as helpful)