Homework week 1 Create the function “quadratic” 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.
sprintf("Starts at %s", Sys.time())
## [1] "Starts at 2019-12-25 13:41:58"
quadratic <- function(a, b, c)
{
delta = 0
delta <- as.integer(b^2-4*a*c)
if(is.na(delta)) { delta = 0 }
if(delta > 0){ # first case D>0
x_1 = (-b+sqrt(delta))/(2*a)
x_2 = (-b-sqrt(delta))/(2*a)
sprintf("roots are %s %s ", x_1, x_2)
}
else if(delta == 0){ # second case D=0
x = -b/(2*a)
sprintf("root is %s ", x)
}
else {"There are no real roots."} # third case D<0
}
a <-as.integer(readline(prompt="Enter a: "))
## Enter a:
b <-as.integer(readline(prompt="Enter b: "))
## Enter b:
c <-as.integer(readline(prompt="Enter c: "))
## Enter c:
sprintf("Quadratic Equation: %sx^2 + %sb + %s = 0",a,b,c)
## [1] "Quadratic Equation: NAx^2 + NAb + NA = 0"
quadratic(a, b, c)
## [1] "root is NA "
sprintf("Ends at %s", Sys.time())
## [1] "Ends at 2019-12-25 13:41:58"