Write a loop that calculates 12-factorial
factorial <- function(num)
{
total <- num
for(i in (num-1):1)
{
total <- total*i
}
return(total)
}
factorial(12)
## [1] 479001600
Show how to create a numeric vector that contains sequence from 20 to 50 by 5
sequence <- seq(20,50, by=5)
sequence
## [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.
factorial <- function(a, b, c)
{
d <- b^2-4*a*c
if(d>0)
{
x1 <- (-1*b + sqrt(b^2-4*a*c)) / (2*a)
x2 <- (-1*b - sqrt(b^2-4*a*c)) / (2*a)
print("values of x: ")
print(x1)
print(x2)
} else if(d==0)
{
x <- (-1*b + sqrt(b^2-4*a*c)) / (2*a)
print("value of x: ")
print(x)
} else
{
print("No values for x")
}
}
factorial(1,-3,-4)
## [1] "values of x: "
## [1] 4
## [1] -1