1. Find 12 factorial
factorial <- function(x) {
if x<=0 {
return 0
}
fact <- 1
for (i in 1:x) {
fact <- fact * i
}
return fact
}
x <- factorial(12)
print(x)
## [1] 479001600
2. Create a numeric vector containing numbers from 20 to 50 in a sequence of 5
print(seq(from = 20, to = 50, by = 5))
## [1] 20 25 30 35 40 45 50
3.Create the function “factorial” 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.
factorialQ <- function(a, b, c) {
if(a == 0) {
return("This is not a quadratic function")
}
## going by quadratic formula
delta = b^2 - 4*a*c
if(delta >0) {
x <- (-b + sqrt(delta)) / (2*a)
y <- (-b - sqrt(delta)) /(2*a)
result <- paste("Two possible real values = ", x, " and ", y)
return(result)
} else if(delta == 0) {
x <- -b /2*a
result <- paste("real root is ", x)
return(result)
} else {
return("There are no real roots")
}
}
result <- factorialQ(4,0,-9)
print(result)
## [1] "Two possible real values = 1.5 and -1.5"