R Program to Convert Celsius to Fahrenheit

1. Introduction

Temperature is a fundamental physical quantity that describes the degree of hotness or coldness of an object or environment. Two of the most commonly used temperature scales are Celsius (°C) and Fahrenheit (°F). In this guide, we will craft an R program that aids in converting a temperature from Celsius to Fahrenheit.

2. Program Goal

The primary aim of the program is to solicit the user to input a temperature in Celsius. 

Post obtaining the temperature, the program will convert it to Fahrenheit and subsequently display the Fahrenheit equivalent to the user.

3. Code Program

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

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

# Display the Fahrenheit equivalent to the user
cat("The equivalent temperature in Fahrenheit is:", fahrenheit, "°F\n")

Output:

Enter the temperature in Celsius: 25
The equivalent temperature in Fahrenheit is: 77 °F

4. Step By Step Explanation

1. Our program starts by prompting the user to input the temperature in Celsius using the cat function.

2. The user's input is captured by readLines(n=1). This input is transformed into a numeric format via the as.numeric function and then stored in the celsius variable.

3. Using the conversion formula ( F = C times frac{9}{5} + 32 ), the program determines the Fahrenheit equivalent of the provided Celsius temperature. The resulting value is saved in the fahrenheit variable.

4. The last step involves the program presenting the computed Fahrenheit temperature to the user with the help of the cat function.

Comments