0% found this document useful (0 votes)
851 views2 pages

Exercise 1

The document presents a practical exercise for converting temperatures between Fahrenheit and Celsius using R programming. It includes a code snippet that prompts the user for temperature and unit, performs the conversion based on the input, and displays the result. Sample outputs demonstrate the program's functionality with user inputs.

Uploaded by

monika03xxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
851 views2 pages

Exercise 1

The document presents a practical exercise for converting temperatures between Fahrenheit and Celsius using R programming. It includes a code snippet that prompts the user for temperature and unit, performs the conversion based on the input, and displays the result. Sample outputs demonstrate the program's functionality with user inputs.

Uploaded by

monika03xxx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like