R Program to create a simple calculator

How to create a simple calculator

Here we are explaining how to write an R program to create a simple calculator. A simple calculator, displaying the different arithmetical operations. Mainly the operations are

  • Addition
  • Subtraction
  • Multiplication
  • Division.

So to make a Calculator we need a choice to select the Operation and do that selected Operation to get the result.

How to implement a simple calculator in R Program

Given below are the steps which are used in the R program to create a simple calculator. In this R program, we take the operation choice and two numbers from the user into the variables n1,n2, choice respectively. And the corresponding user-defined function is called as per the choice. Here we are using switch branching to execute a particular function.The choice operators ‘+’, ‘-’, ’*‘,’/ ’ are used corresponding to user choice 1,2,3,4.

ALGORITHM

  • STEP 1 : prompting appropriate messages to the user
  • STEP 2 : take user input using readline() into variables n1, n2, choice
  • STEP 3 : Define userdefined functions for addition,subtraction,multiplication and division
  • STEP 4 : give the option to switch
  • STEP 5 : call the corresponding function as per choice
  • STEP 6 : print the result

R Source Code

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]: "))
# n1 = as.integer(readline(prompt="Enter first number: "))
# n2 = as.integer(readline(prompt="Enter second number: "))

choice = 1
n1 = 75
n2 = 19

operator <- switch(choice,"+","-","*","/")

result <- switch(choice, add(n1, n2), subtract(n1, n2), multiply(n1, n2), divide(n1, n2))

print(paste(n1, operator, n2, "=", result))
## [1] "75 + 19 = 94"
# 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]: "))
# n1 = as.integer(readline(prompt="Enter first number: "))
# n2 = as.integer(readline(prompt="Enter second number: "))

choice = 2
n1 = 75
n2 = 19

operator <- switch(choice,"+","-","*","/")

result <- switch(choice, add(n1, n2), subtract(n1, n2), multiply(n1, n2), divide(n1, n2))

print(paste(n1, operator, n2, "=", result))
## [1] "75 - 19 = 56"
# 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]: "))
# n1 = as.integer(readline(prompt="Enter first number: "))
# n2 = as.integer(readline(prompt="Enter second number: "))

choice = 3
n1 = 9
n2 = 13

operator <- switch(choice,"+","-","*","/")

result <- switch(choice, add(n1, n2), subtract(n1, n2), multiply(n1, n2), divide(n1, n2))

print(paste(n1, operator, n2, "=", result))
## [1] "9 * 13 = 117"
# 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]: "))
# n1 = as.integer(readline(prompt="Enter first number: "))
# n2 = as.integer(readline(prompt="Enter second number: "))

choice = 4
n1 = 75
n2 = 25

operator <- switch(choice,"+","-","*","/")

result <- switch(choice, add(n1, n2), subtract(n1, n2), multiply(n1, n2), divide(n1, n2))

print(paste(n1, operator, n2, "=", result))
## [1] "75 / 25 = 3"