Introduction
Simple interest is a quick and easy method of calculating the interest charge on a loan or an investment. The formula for simple interest is:
[
\text{Simple Interest} = \frac{\text{Principal} \times \text{Rate} \times \text{Time}}{100}
]
This guide will walk you through writing an R program to calculate simple interest based on user input.
Problem Statement
Create an R program that:
- Prompts the user to enter the principal amount, the rate of interest, and the time period.
- Calculates the simple interest using the provided formula.
- Displays the calculated simple interest.
Example:
- Input:
- Principal: 10000
- Rate of Interest: 5%
- Time: 3 years
- Output: Simple Interest = 1500
Solution Steps
- Read the Principal Amount: Use the
readline()function to take the principal amount as input from the user. - Read the Rate of Interest: Use the
readline()function to take the rate of interest as input from the user. - Read the Time Period: Use the
readline()function to take the time period as input from the user. - Convert Inputs to Numeric: Convert the inputs from character strings to numeric values using the
as.numeric()function. - Calculate the Simple Interest: Use the formula to calculate the simple interest.
- Display the Simple Interest: Use the
print()function to display the calculated simple interest.
R Program
# R Program to Calculate Simple Interest
# Author: Ramesh Fadatare
# Step 1: Read the principal amount from the user
principal <- as.numeric(readline(prompt = "Enter the principal amount: "))
# Step 2: Read the rate of interest from the user
rate <- as.numeric(readline(prompt = "Enter the rate of interest: "))
# Step 3: Read the time period from the user
time <- as.numeric(readline(prompt = "Enter the time period in years: "))
# Step 4: Calculate the simple interest
simple_interest <- (principal * rate * time) / 100
# Step 5: Display the simple interest
print(paste("Simple Interest =", simple_interest))
Explanation
Step 1: Read the Principal Amount
- The
readline()function prompts the user to enter the principal amount. The input is read as a string and converted to a numeric value usingas.numeric().
Step 2: Read the Rate of Interest
- The
readline()function prompts the user to enter the rate of interest, which is also converted to a numeric value.
Step 3: Read the Time Period
- The
readline()function prompts the user to enter the time period in years, which is then converted to a numeric value.
Step 4: Calculate the Simple Interest
- The simple interest is calculated using the formula:
(Principal * Rate * Time) / 100.
Step 5: Display the Simple Interest
- The
print()function is used to display the calculated simple interest.
Output Example
Example:
Enter the principal amount: 10000
Enter the rate of interest: 5
Enter the time period in years: 3
[1] "Simple Interest = 1500"
Conclusion
This R program demonstrates how to calculate simple interest based on user-provided input for the principal amount, rate of interest, and time period. It covers essential programming concepts such as user input, arithmetic calculations, and output formatting, making it a practical example for beginners learning R programming. Understanding how to implement basic financial calculations like this is a valuable skill in both academic and professional settings.