Solutions to the Questions

  1. Write a loop that calculates 12-factorial
factorial.count = 1
answer = 1

while (factorial.count <= 12){
  answer = answer * factorial.count
  factorial.count = factorial.count + 1
}

print(paste("12! = ", answer))
## [1] "12! =  479001600"
  1. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
sample.vector <- vector()
initial.counter = 20

while (initial.counter <= 50){
  sample.vector <- append(sample.vector,initial.counter)
  initial.counter = initial.counter + 5
}

print(sample.vector)
## [1] 20 25 30 35 40 45 50
  1. 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.
# A function called "Factorial" that utilizes 3 inputs to solve a quadratic equation
# ax^2+bx+c=0
# answer x1 = (-b+sqrt(b^2-4*a*c))/(2*a)
# answer x2 = (-b-sqrt(b^2-4*a*c))/(2*a)
# However, the number D under the sqrt cannot be negative, as you cannot take the square root of a negative
# Therefore, will need if, else if, and else statement
# if b^2-4*a*c == 0, there is only one answer and the formula becomes -b/(2*a)

a = 1
b = 3
c = -32

D <- b^2-4*a*c

Factorial <- function(a,b,c){
  if(D > 0){
    x1 = ((-b+sqrt(D))/(2*a))
    x2 = ((-b-sqrt(D))/(2*a))
    Answer <- c(x1,x2)
    return(Answer)
  } else if (D == 0){
    Answer <- (-b/(2*a))
    return(Answer)
  } else{
    return("There are no answers to this question.")
  }
}

Answer.Quadratics <- Factorial(a,b,c)
print(Answer.Quadratics)
## [1]  4.35235 -7.35235

RPubs Link to my Account: User: jcp9010

https://rpubs.com/jcp9010