R Program to Make a Simple Calculator

Introduction

A simple calculator performs basic arithmetic operations such as addition, subtraction, multiplication, and division. In R, you can create an interactive calculator that takes user input and performs the desired operation. This guide will walk you through writing an R program that functions as a simple calculator.

Problem Statement

Create an R program that:

  • Prompts the user to choose an operation:
    • Addition
    • Subtraction
    • Multiplication
    • Division
  • Prompts the user to enter two numbers.
  • Performs the chosen operation on the input numbers.
  • Displays the result of the computation.

Example:

  • Input:
    • Select operation: Addition
    • Enter first number: 10
    • Enter second number: 5
  • Output:
    • Result: 15

Solution Steps

  1. Display Operation Options: Present the user with a menu of available operations.
  2. Read User’s Choice: Use the readline() function to capture the user’s selected operation.
  3. Prompt for Numbers: Use readline() to input two numbers from the user and convert them to numeric values.
  4. Perform Calculation: Use conditional statements (if, else if, else) to perform the operation based on the user’s choice.
  5. Display Result: Output the result using the print() function.
  6. Handle Invalid Input: Include error handling for invalid operation choices and division by zero.

R Program

# R Program to Make a Simple Calculator # Author: Ramesh Fadatare # Step 1: Display available operations cat("Select operation:\n") cat("1. Addition (+)\n") cat("2. Subtraction (-)\n") cat("3. Multiplication (*)\n") cat("4. Division (/)\n") # Step 2: Read user's choice choice <- as.integer(readline(prompt = "Enter choice [1/2/3/4]: ")) # Validate choice input if(choice %in% 1:4){ # Step 3: Read two numbers from the user num1 <- as.numeric(readline(prompt = "Enter first number: ")) num2 <- as.numeric(readline(prompt = "Enter second number: ")) # Step 4: Perform calculation based on user choice result <- switch(choice, num1 + num2, num1 - num2, num1 * num2, if(num2 != 0) num1 / num2 else "Error: Division by zero") # Step 5: Display the result operation <- switch(choice, "+", "-", "*", "/") if(result == "Error: Division by zero"){ print(result) } else { print(paste(num1, operation, num2, "=", result)) } } else { print("Invalid choice. Please select a valid operation.") } 

Explanation

Step 1: Display Available Operations

  • The cat() function prints a menu of operations for the user to choose from:
    Select operation: 1. Addition (+) 2. Subtraction (-) 3. Multiplication (*) 4. Division (/) 

Step 2: Read User’s Choice

  • readline() prompts the user to enter their choice, which is then converted to an integer using as.integer().
  • The program checks if the entered choice is between 1 and 4 using the %in% operator.

Step 3: Read Two Numbers from the User

  • readline() is used again to prompt the user for two numbers.
  • The inputs are converted to numeric values using as.numeric() for computation.

Step 4: Perform Calculation Based on User Choice

  • The switch() function selects the appropriate operation based on the user’s choice:
    • 1: Addition
    • 2: Subtraction
    • 3: Multiplication
    • 4: Division (includes a check to prevent division by zero)
  • If the user attempts to divide by zero, the program sets the result to an error message.

Step 5: Display the Result

  • Another switch() function assigns the correct operation symbol for display purposes.
  • The print() function outputs the result in a formatted string.
  • If there was an error (e.g., division by zero), the error message is displayed instead.

Error Handling

  • The program includes checks to handle invalid operation choices and division by zero to prevent runtime errors and provide user-friendly feedback.

Output Example

Example 1: Addition

Select operation: 1. Addition (+) 2. Subtraction (-) 3. Multiplication (*) 4. Division (/) Enter choice [1/2/3/4]: 1 Enter first number: 10 Enter second number: 5 [1] "10 + 5 = 15" 

Example 2: Division

Select operation: 1. Addition (+) 2. Subtraction (-) 3. Multiplication (*) 4. Division (/) Enter choice [1/2/3/4]: 4 Enter first number: 20 Enter second number: 4 [1] "20 / 4 = 5" 

Example 3: Division by Zero

Select operation: 1. Addition (+) 2. Subtraction (-) 3. Multiplication (*) 4. Division (/) Enter choice [1/2/3/4]: 4 Enter first number: 20 Enter second number: 0 [1] "Error: Division by zero" 

Example 4: Invalid Choice

Select operation: 1. Addition (+) 2. Subtraction (-) 3. Multiplication (*) 4. Division (/) Enter choice [1/2/3/4]: 5 [1] "Invalid choice. Please select a valid operation." 

Conclusion

This R program demonstrates how to create a simple calculator that performs basic arithmetic operations based on user input. It covers essential programming concepts such as user input handling, conditional statements, and basic error checking. This example is beneficial for beginners learning how to interact with users and perform calculations in R.

Leave a Comment

Scroll to Top