quad <- function(a, b, c) {
# Calculate the discriminant
disc <- b^2 - 4*a*c
# Check the discriminant for different conditions
if (disc > 0) {
root1 <- (-b + sqrt(disc)) / (2*a)
root2 <- (-b - sqrt(disc)) / (2*a)
print(paste("The solutions are", root1, "and", root2))
} else if (disc == 0) {
root <- -b / (2*a)
print(paste("The solution is", root))
} else {
print("The equation has no real roots")
}
}
print(quad(1,2,1))
## [1] "The solution is -1"
## [1] "The solution is -1"
print(quad(1,6,5))
## [1] "The solutions are -1 and -5"
## [1] "The solutions are -1 and -5"
print(quad(1,1,1))
## [1] "The equation has no real roots"
## [1] "The equation has no real roots"
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.