Write a program to perform Celsius to Fahrenheit and Fahrenheit to Celsius conversion. In
the start ask to user if user wants to conversion from Celsius to Fahrenheit or From
Fahrenheit to Celsius.Solve this
question using switch case.
Answers
Answer:
/* C Program to convert temperature from Fahrenheit to Celsius and vice versa.*/
#include <stdio.h>
int main()
{
float fh,cl;
int choice;
printf("\n1: Convert temperature from Fahrenheit to Celsius.");
printf("\n2: Convert temperature from Celsius to Fahrenheit.");
printf("\nEnter your choice (1, 2): ");
scanf("%d",&choice);
if(choice ==1){
printf("\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("Temperature in Celsius: %.2f",cl);
}
else if(choice==2){
printf("\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("Temperature in Fahrenheit: %.2f",fh);
}
else{
printf("\nInvalid Choice !!!");
}
return 0;
}
This program is in Java. I have also added all the possible outputs ^_^
import java.util.Scanner;
public class CelciusToFahrenheit{
public static void main (String [] args){
Scanner sc = new Scanner(System.in);
System.out.println("Press 0 for Celcius to Fahrenheit and 1 for Vice-Versa");
double choice = sc.nextDouble();
if (choice == 0){
System.out.print("Enter the temperature in Celcius: ");
double tempC = sc.nextDouble();
double tempF = (tempC*1.8) + 32;
System.out.println("The temperature in Fahrenheit: " + tempF + "°F");}
if (choice == 1){
System.out.print("Enter the temperature in Fahrenheit: ");
double tempF = sc.nextDouble();
double tempC = (tempF - 32)/1.8;
System.out.println("The temperature in Celcius: " + tempC + "°C");}
if (choice != 0 && choice != 1)
System.out.println("Enter only 1 or 2 :(");
}
}
Output:
(i)
Press 1 for Celcius to Fahrenheit and 2 for Vice-Versa
123
Enter only 1 or 2 :(
(ii)
Press 1 for Celcius to Fahrenheit and 2 for Vice-Versa
1
Enter the temperature in Celcius: 37
The temperature in Fahrenheit: 98.60000000000001°F
(iii)
Press 1 for Celcius to Fahrenheit and 2 for Vice-Versa
2
Enter the temperature in Fahrenheit: 98.6
The temperature in Celcius: 36.99999999999999°C