R Program to Make a Simple Calculator : Example to create a simple calculator to add, subtract, multiply and divide. to create a simple calculator that can add, subtract, multiply and divide two numbers entered by the user.
Simple Calculator in R
# Program make a simple calculator that can add, subtract, multiply and divide using 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)
}
# take input from the user
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))
R Program to Make a Simple Calculator Output
"Select operation." "1.Add" "2.Subtract" "3.Multiply" "4.Divide" Enter choice[1/2/3/4]: 4 Enter first number: 40 Enter second number: 4 "40 / 4 = 10"
Hope you make in R Program to Make a Simple Calculator.if you face any problem kindly Contact us.Thank you.