Created
January 22, 2022 22:15
-
-
Save CodeMaster7000/3b1da69eec3a16be386a9d2067b88d40 to your computer and use it in GitHub Desktop.
A simple calculator coded in R, performing the 4 basic functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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