Question 1: Write a loop that calculates 12-factorial
for(i in 1:12)
{
ans.remainder <- 12 %% i
if(ans.remainder == 0)
{
print(i)
}
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 6
## [1] 12
Question 2: Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
v_seq_by_5 <- seq(from=20, to=50, by = 5)
v_seq_by_5
## [1] 20 25 30 35 40 45 50
class(v_seq_by_5)
## [1] "numeric"
Question 3: 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.
quadratic <- function(a, b, c)
{
plus.quad.eq <- (-b + sqrt(b^2 - (4 * a * c)))/ 2 * a
minus.quad.eq <- (-b - sqrt(b^2 - (4 * a * c)))/ 2 * a
sprintf("Plus result: %s and Minus result: %s", plus.quad.eq, minus.quad.eq)
}
a <- 1
b <- 5
c <- 6
quadratic(a, b, c)
## [1] "Plus result: -2 and Minus result: -3"