R Program to Convert Fahrenheit to Celsius

1. Introduction

While the Celsius (°C) scale is widely used across the globe for most temperature measurements, the Fahrenheit (°F) scale still finds prominence, especially in countries like the United States. The relationship between these two scales is such that often there's a need to convert from one to the other. The formula for converting Fahrenheit to Celsius is C = (F - 32) times frac{5}{9}. In this tutorial, we will create an R program to help in converting a temperature from Fahrenheit to Celsius.

2. Program Goal

This program aims to ask the user to provide a temperature in Fahrenheit. 

Once the temperature is entered, the program will compute its Celsius counterpart and display the result to the user.

3. Code Program

# Prompt the user to enter temperature in Fahrenheit
cat("Enter the temperature in Fahrenheit: ")
fahrenheit <- as.numeric(readLines(n=1))

# Convert the Fahrenheit temperature to Celsius
celsius <- (fahrenheit - 32) * (5/9)

# Display the Celsius equivalent to the user
cat("The equivalent temperature in Celsius is:", celsius, "°C\n")

Output:

Enter the temperature in Fahrenheit: 100
The equivalent temperature in Celsius is: 37.77778 °C

4. Step By Step Explanation

1. The program is initiated with a prompt for the user to key in the temperature in Fahrenheit. This is achieved using the cat function.

2. readLines(n=1) captures the user's input, which is then converted to a numeric format using the as.numeric function. This numeric value is stored in the fahrenheit variable.

3. The program then employs the formula ( C = (F - 32) times frac{5}{9} ) to convert the provided Fahrenheit temperature to Celsius. The resultant value is assigned to the celsius variable.

4. Finally, the program uses the cat function to display the computed Celsius temperature to the user, thus concluding its execution.

Comments