Following code demonstrates how to create a calculator that performs addition, subtraction,
multiplication, modulus and division:
addition=function(a,b){
return(a+b)}
subtraction=function(a,b){
return(a-b)}
multiplication=function(a,b){
return(a*b)}
division=function(a,b){
return(a/b)}
Modulus=function(a,b){
return(a%%b)}
print("Enter the operation")
print("1.Addition")
print("2.Subtraction")
print("3.Division")
print("4.Multiplication")
print("5.Modulus")
choice=as.integer(readline(prompt="Enter choice[1/2/3/4/5]:"))
num1=as.integer(readline(prompt="Enter first number:"))
num2=as.integer(readline(prompt="Enter second number:"))
operator=switch(choice,"+","-","/","*","%%")
result=switch(choice,addition(num1,num2),subtraction(num1,num2),division(num1,num2),multiplication(num1,num2),Modulus(num1,num2))
print(paste(num1,operator,num2,"=",result))