Q1
- Write a loop that calculates 12-factorial:
for(i in 1:12)
{fact<-factorial(i)}
print(fact)
## [1] 479001600
Q2
- Show how to create a numeric vector that contains the sequence from
20 to 50 by 5:
print(seq(from=20, to=50, by=5)
- 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.
# i couldn't get the user input to feed into the function so i hardcoded numbers below
# how would the user input be passed into the function?
#a<- as.integer(readline(prompt="Enter value for variable a: "))
#b<- as.integer(readline(prompt="Enter value for variable b: "))
#c<- as.integer(readline(prompt="Enter value for variable c: "))
quadratic = function(a,b,c){
disc<-(b^2)-(4*a*c)
if (disc<0){
return(paste("Discriminate cannot be negative. Please enter new numbers:"))
}
else if(disc>0){
x1=-(b+sqrt(b^2-4*a*c))/(2*a)
x2=-(b-sqrt(b^2-4*a*c))/(2*a)
return(c(x1,x2))
}
else "blank"}
quadratic(1,100,2)
## [1] -99.979996 -0.020004