4. Show your output that only even numbers from 80 to 115 get printed. Hint. You can use modulo (%%) in Chapter 1. Even numbers have the remainder 0 after division of the number by 2. You start with the following code for a) and b): number <- 80
a) Use repeat loop
b) Use while loop You start with the following code for c): number <- seq(from = 80, to = 115)
c) [ Use for loop
a) Use repeat loop
number <- 80
repeat {
if (number %% 2 == 0){
print(number)
}
number = number + 1
if (number == 115){
break
}
}
b) Use while loop
#R version 3.3.2
number <- 80
while (number < 115) {
if(number %% 2 ==0)
print(number)
number = number+1
}
c) Use for loop
#R version 3.3.2
number <- seq(from = 80, to = 115)
for (val in number) {
if(val %% 2 == 0)
print(val)
}
OUTPUT:
80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114
Get Answers For Free
Most questions answered within 1 hours.