1. Write a loop that calculates 12-factorial

x<- 12
factorial<- 1
j<- 0

for(i in 1:x){
  if(j == 0){
    print(paste(j, "factorial is", factorial))
  }
  j = j + 1
  
  factorial = factorial * j
  print(paste(j, "factorial is", factorial))
}
## [1] "0 factorial is 1"
## [1] "1 factorial is 1"
## [1] "2 factorial is 2"
## [1] "3 factorial is 6"
## [1] "4 factorial is 24"
## [1] "5 factorial is 120"
## [1] "6 factorial is 720"
## [1] "7 factorial is 5040"
## [1] "8 factorial is 40320"
## [1] "9 factorial is 362880"
## [1] "10 factorial is 3628800"
## [1] "11 factorial is 39916800"
## [1] "12 factorial is 479001600"
cat("\n\n")
print(paste("The fatorial of ", x, "is", factorial))
## [1] "The fatorial of  12 is 479001600"

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

sequence <- vector() #empty numeric vector
for(j in 20:50){

  if(j %% 5 == 0)
  {
    sequence <-append(sequence, j)
  
  }

}
sequence #displays new items added to the numeric vector
## [1] 20 25 30 35 40 45 50

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.

quadratic <-function(a, b, c){

  if( a == 0)
  {
    print("This equation is linear, not quadratic.")
  
  }else if(a != 0)
  {
    #tells what type of root the equation has
    discriminant = (b * b) - 4 * a * c 
    
    if(discriminant > 0)
    { 
      print("Roots are real and there are two solutions.")
      x1 = (-b + (sqrt(discriminant)))/ 2*a
      x2 = (-b - (sqrt(discriminant))) / 2*a
      
      print(paste("x1 = ", x1, "and x2 = ", x2))
      
    }else if(discriminant == 0)
    {
      print("Roots are real and equal. There is only one solution.")
      x1 = x2 = (-b + (sqrt(discriminant))) / 2*a
      
      print(paste("x1 = ", x1))
      
    }else if(discriminant < 0)
    {
      print("Roots are complex/imaginary and therefore there is no solution.")
      
      x1 = -b + sqrt(as.complex(discriminant))/ 2*a
      x2 = -b - sqrt(as.complex(discriminant)) / 2*a
      
      print(paste("x1 =", x1, "and", "x2 = ", x2))
      
      
      
      
    }
    
  }
}


#calling function quadratic to solve equations
quadratic(1, -2, 1)
## [1] "Roots are real and equal. There is only one solution."
## [1] "x1 =  1"
quadratic(1, 0, -1)
## [1] "Roots are real and there are two solutions."
## [1] "x1 =  1 and x2 =  -1"
quadratic(1, 4, 5)
## [1] "Roots are complex/imaginary and therefore there is no solution."
## [1] "x1 = -4+1i and x2 =  -4-1i"
quadratic(1, 0, 25)
## [1] "Roots are complex/imaginary and therefore there is no solution."
## [1] "x1 = 0+5i and x2 =  0-5i"