1. Write a loop that calculates 12-factorial.

fact <- function(num) {
  fac = 1
  if (num < 0) {
    print("Try Positive Integers.")
  } else if(num == 0 ) {
    print("Factorial of 0 is 1.")
  } else {
    for( i in 1:num) {
      fac = fac * i
    }
    paste("Factorial of ", num, "is", fac)
  }
}

fact(12)
## [1] "Factorial of  12 is 479001600"

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

vec <- seq(from=20, to=50, by=5)
vec
## [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.

quad_func <- function(a, b, c) {
  
  discri <- discriminant(a, b, c)
  
  if (discri == 0) {
    x <- -b/(2*a)
    x
  } else if (discri > 0) {
    x1 <- (-b + sqrt(discri)) / (2*a)
    x2 <- (-b - sqrt(discri)) / (2*a)
    
    paste(x1, x2)
  } else {
    print("No result since discriminant is less than 0.")
  }
}

discriminant <- function(a, b, c) {
  return (b^2 - 4*a*c)
}

# discriminant is less than 0
quad_func(2,3,4)
## [1] "No result since discriminant is less than 0."
# discriminant equals 0
quad_func(4, 12, 9)
## [1] -1.5
# discriminant is greater than 0
quad_func(4, 12, 6)
## [1] "-0.633974596215561 -2.36602540378444"