PRACTICAL – XII (R PROGRAMMING LAB ) – 23CAU529
EXERCISE – 1
Program to convert the given temperature from Farenheit to Celsius and vice versa depending
upon user’s choice.
Program Code :
# Get temperature input from the user
temp_input <- readline(prompt="Enter temperature: ")
temp <- as.numeric(temp_input)
# Get unit input from the user (C or F)
unit_input <- readline(prompt="Enter unit (C or F): ")
# Perform conversion based on the unit
if (toupper(unit_input) == "C") {
# Convert Celsius to Fahrenheit
fahrenheit <- (temp * 9/5) + 32
cat(temp, "Celsius is equal to", fahrenheit, "Fahrenheit\n")
} else if (toupper(unit_input) == "F") {
# Convert Fahrenheit to Celsius
celsius <- (temp - 32) * 5/9
cat(temp, "Fahrenheit is equal to", celsius, "Celsius\n")
} else {
cat("Invalid unit. Please enter C or F.\n")
}
OUTPUT 1 :
Enter temperature: 56
Enter unit (C or F): C
56 Celsius is equal to 132.8 Fahrenheit
OUTPUT 2 :
Enter temperature: 89
Enter unit (C or F): F
89 Fahrenheit is equal to 31.66667 Celsius