Question1
factorial <- 1
for(q in 1:12)
{
factorial = factorial * q
}
print(paste("12! = ", factorial))
## [1] "12! = 479001600"
Question 2
numVec <- seq(20,50,5)
numVec
## [1] 20 25 30 35 40 45 50
Question 3
quadratic <- function(a,b,c)
{
if(a == 0)
{
return("This is not a quadratic equation")
}
discriminant <- (b ^ 2) - (4 * a * c)
if(discriminant == 0)
{
root1 = (- b / (2 * a))
return(print(paste("There is only one solution = ", root1)))
}
else if(discriminant > 0)
{
root1 = (- b + sqrt(discriminant)) / (2 * a)
root2 = (- b - sqrt(discriminant)) / (2 * a)
return(print(paste("There are two solutions = ", root1, "and ", root2)))
}
else
{
root1 = (complex(real = - b / (2 * a), imaginary = (sqrt(-discriminant) / (2 * a))))
root2 = (complex(real = - b / (2 * a), imaginary = (- sqrt(-discriminant) / (2 * a))))
return(print(paste("There are two complex solutions = ", root1, "and ", root2)))
}
}
quadratic(2,3,1)
## [1] "There are two solutions = -0.5 and -1"
Another Example for Question 3 showing complex solutions
quadratic(4,6,3)
## [1] "There are two complex solutions = -0.75+0.433012701892219i and -0.75-0.433012701892219i"
Another Example for Question 3 showing a = 0
quadratic(0,2,-3)
## [1] "This is not a quadratic equation"
Another Example for Question 3 showing only one solution
quadratic(2,4,2)
## [1] "There is only one solution = -1"