Computer Applications

Using switch case, write a program to convert temperature from Fahrenheit to Celsius and Celsius to Fahrenheit.
Fahrenheit to Celsius formula: (32°F - 32) x 5/9
Celsius to Fahrenheit formula: (0°C x 9/5) + 32

Java

Java Conditional Stmts

17 Likes

Answer

import java.util.Scanner;

public class KboatTemperature
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type 1 for Fahrenheit to Celsius");
        System.out.println("Type 2 for Celsius to Fahrenheit");
        int choice = in.nextInt();
        double ft = 0.0, ct = 0.0;

        switch (choice) {
            case 1:
                System.out.print("Enter temperature in Fahrenheit: ");
                ft = in.nextDouble();
                ct = (ft - 32) * 5 / 9.0;
                System.out.println("Temperature in Celsius: " + ct);
                break;

            case 2:
                System.out.print("Enter temperature in Celsius: ");
                ct = in.nextDouble();
                ft = 9.0 / 5.0 * ct + 32;
                System.out.println("Temperature in Fahrenheit: " + ft);
                break;

            default:
                System.out.println("Incorrect Choice");
                break;
        }
    }
}

Output

Answered By

7 Likes


Related Questions