MSDS BRIDGE R WEEK 1 HOME WORK / YOHANNES DEBOCH output: html_document —

  1. Write a loop that calculates 12 factorial
# Declare res 
res <- 1

for(i in 1:12){
  # Do multiplication
  res  <- res*i
}
# Print the result
print(res)
## [1] 479001600
  1. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5
# Create a numeric vector
num.vec <- seq(20,50,5)
num.vec
## [1] 20 25 30 35 40 45 50
  1. Crate the function “factorial” that takes a trio of a, b and c and solve the quadratic equation. The function should print as output the solutions.
# Crating functions 
factorial <- function(a, b, c){
  # Find the solution
  sol1 <- (-b+sqrt(b^2-4*a*c))/(2*a)
  sol2 <- (-b-sqrt(b^2-4*a*c))/(2*a)
  # Print solution
  print(sol1)
  print(sol2)
}

# Test the functions
factorial(1, 3, -4)
## [1] 1
## [1] -4