knitr::opts_chunk$set(echo = TRUE)
#1. Write a loop that calculates 12-factorial
#NOTE: Would replace 1:12 with 1:n and uncomment lines 19 and 25 to base the factorial on input
#n = as.integer(readline(prompt="Input number: "))
f = 1
for(x in 1:12) {
f = f * x
}
print(paste("12- factorial =",f))
## [1] "12- factorial = 479001600"
#print(paste(n,"- factorial =",f))
#2. Show how to create a numeric vector that contains the sequence from 20 to 50 by 5.
vector = c(seq(from = 20, to = 50, by = 5))
print(vector)
## [1] 20 25 30 35 40 45 50
#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.
a = as.numeric(readline(prompt="Input number a: "))
## Input number a:
b = as.numeric(readline(prompt="Input number b: "))
## Input number b:
c = as.numeric(readline(prompt="Input number c: "))
## Input number c:
quadratic<-function(a,b,c){
if(delta(a,b,c)>0){
x1 = (-b+sqrt(delta(a,b,c)))/(2*a)
x2 = (-b+sqrt(delta(a,b,c)))/(2*a)
quadratic = c(x1,x2)
}
else if(delta(a,b,c)==0){
x=-b/(2*a)
}
else {"No Roots"}
}
delta<-function(a,b,c){
b^2-4*a*c
}
print(quadratic(a,b,c))
## Error in if (delta(a, b, c) > 0) {: missing value where TRUE/FALSE needed