Jian Quan Chen
##1. Write a loop that calculates 12-factorial
factorial <- 1
for (x in 12:1) {
factorial <- factorial * x
}
print(factorial)
## [1] 479001600
#2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
numVec <- seq(from = 20, to = 50, by = 5)
print(numVec)
## [1] 20 25 30 35 40 45 50
#3. Create the function “quadratic” that takes a trio of input numbers a, b, and c and solve the quadratic equation. The function should print as output the two solutions.
quadratic <- function(a, b, c) {
discriminant <- b^2 - 4*a*c
if (discriminant >= 0) {
solution1 <- (-b + sqrt(discriminant)) / (2*a)
solution2 <- (-b - sqrt(discriminant)) / (2*a)
print(solution1)
print(solution2)
}
else {
print("The equation has no real solution")
}
}
quadratic(2,10,-3)
## [1] 0.2838822
## [1] -5.283882
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.