Write a loop that calculates 12-factorial
fact <- 1
for (i in 1:12)
fact <- fact*i
print(paste(" 12! is ",fact))
## [1] " 12! is 479001600"
Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
numVect <- seq(20, 50, 5)
print(numVect)
## [1] 20 25 30 35 40 45 50
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) {
print("The quadratic equation has no real root")
}else if (discriminant == 0){
root <- (-b) / (2*a)
print(paste("The quadratic equation has only one root ",root))
}else{
root1 <- (-b + sqrt(discriminant)) / (2*a)
root2 <- (-b - sqrt(discriminant)) / (2*a)
print(paste("First root is ",root1," and Second root is ",root2))
}
}
quadratic(2,5,-3)
## [1] "First root is 0.5 and Second root is -3"