Question 1

  1. Write a loop that calculates 12-factorial 12! = 12 x 11 X 10 x 9…x 3 x 2 x1
find_factorial <- function(m)
{
  no <- as.integer(m)
  fact = 1
  for(i in 1:no)
  {
    fact = fact*i
  }
  print(fact)
}
find_factorial(12)
## [1] 479001600

Question 2

  1. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
sequence_to_find <- function(x,y,z)
{
  answer <- seq(from=x, to=y, by=z)
  print(answer)
}
sequence_to_find(20, 50, 5)
## [1] 20 25 30 35 40 45 50

Question 3

  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.
Quad_solution = function(a, b, c)
{
sol1 <--(b+sqrt(b^2-4*a*c))/(2*a)
sol2 <--(b-sqrt(b^2-4*a*c))/(2*a)
return(c(sol1, sol2))
}
Quad_solution(2,1,0)
## [1] -0.5  0.0