Skip to content

Instantly share code, notes, and snippets.

@CodeMaster7000
Created January 22, 2022 22:15
Show Gist options
  • Select an option

  • Save CodeMaster7000/3b1da69eec3a16be386a9d2067b88d40 to your computer and use it in GitHub Desktop.

Select an option

Save CodeMaster7000/3b1da69eec3a16be386a9d2067b88d40 to your computer and use it in GitHub Desktop.
A simple calculator coded in R, performing the 4 basic functions.
add <- function(x, y) {
return(x + y)
}
subtract <- function(x, y) {
return(x - y)
}
multiply <- function(x, y) {
return(x * y)
}
divide <- function(x, y) {
return(x / y)
}
print("Select operation.")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = as.integer(readline(prompt="Enter choice[1/2/3/4]: "))
num1 = as.integer(readline(prompt="Enter first number: "))
num2 = as.integer(readline(prompt="Enter second number: "))
operator <- switch(choice,"+","-","*","/")
result <- switch(choice, add(num1, num2), subtract(num1, num2), multiply(num1, num2), divide(num1, num2))
print(paste(num1, operator, num2, "=", result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment