R Markdown

R BRIDGE ASSIGNMENT WEEK 1

1 This is the for loop for factorial of 12

factor <- 1
for (i in 1:12){
  factor <- factor * i
}
factor
## [1] 479001600

2 Create a sequence

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

3 Solutions of a quadratic equation

factorial <- function(a,b,c){
  if (a==0){
    print("This is not a quadratic equation!")
  }else{
    delta = b*b - 4 * a * c
    if(delta < 0){
      print("There are no solutions!")
    }else if(delta == 0){
      x0 = -b/(2 * a)
      print(paste("The only solution is: ", x0))
    }else{
      x1 = (-b  + sqrt(delta))/(2 * a)
      x2 = (-b  - sqrt(delta))/(2 * a)
      print(paste("The two solutions are: ", round(x1,2), 
                  " and ",round(x2,2)))
    }
  }
}


factorial(5,3,-9)
## [1] "The two solutions are:  1.07  and  -1.67"
factorial(0,2,5)
## [1] "This is not a quadratic equation!"
factorial(1,2,1)
## [1] "The only solution is:  -1"
factorial(1, 3,-10)
## [1] "The two solutions are:  2  and  -5"
factorial(1,0,1)
## [1] "There are no solutions!"
factorial(1,-2,1)
## [1] "The only solution is:  1"
factorial(1,-1,-6)
## [1] "The two solutions are:  3  and  -2"
factorial(1,0,-9)
## [1] "The two solutions are:  3  and  -3"