quantity <- 50
if (quantity > 20) {
print("You sold a lot")
} else {
print("Not enough for today")
}[1] "You sold a lot"
Intro to Function Programming
quantity <- 50
if (quantity > 20) {
print("You sold a lot")
} else {
print("Not enough for today")
}[1] "You sold a lot"
category <- "A"
price <- 100
tax_rate <- 0
if (category == "A") {
tax_rate <- 0.08
} else if (category == "B") {
tax_rate <- 0.10
} else if (category == "C") {
tax_rate <- 0.20
}
final_price <- price * (1 + tax_rate)
print(paste("A tax rate of", tax_rate * 100, "% is applied. The total price is", final_price))[1] "A tax rate of 8 % is applied. The total price is 108"
q4 <- c(2, 5, 3, 9, 8, 11, 6)
even_count <- 0
for (number in q4) {
if (number %% 2 == 0) {
even_count <- even_count + 1
}
}
print(even_count)[1] 3
q5 <- 1:8
for (number in q5) {
if (number > 4 && number %% 2 == 0) {
print(number)
} else {
print("Condition not satisfied")
}
}[1] "Condition not satisfied"
[1] "Condition not satisfied"
[1] "Condition not satisfied"
[1] "Condition not satisfied"
[1] "Condition not satisfied"
[1] 6
[1] "Condition not satisfied"
[1] 8
pow <- function(x, y) {
result <- x^y
print(paste(x, "raised to the power of", y, "is", result))
}
pow(8, 2)[1] "8 raised to the power of 2 is 64"