Exercise 1

Write a loop that calculates 12-factorial

fact =1
for (i in  1:12)
    fact = fact*i
fact
## [1] 479001600

Exercise 2

Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.

numVect <- seq(20, 50, 5)
numVect
## [1] 20 25 30 35 40 45 50

Exercise 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) {
        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("Root 1 is ",root1,"Root 2 is ",root2))
        
    }
}
quadratic(2,5,-3)
## [1] "Root 1 is  0.5 Root 2 is  -3"