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

Java Code

Uploaded by

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

Java Code

Uploaded by

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

Java Code

import java.util.Scanner;

public class TemperatureConverter {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter temperature value: ");

double t = input.nextDouble();

input.nextLine();

System.out.print("Enter unit (C, F, or K): ");

String unit = input.nextLine().toUpperCase();

switch (unit) {

case "C": {

double f = (t * 9 / 5) + 32;

double k = t + 273.15;

System.out.printf("Fahrenheit: %.2f°F\n", f);

System.out.printf("Kelvin: %.2fK\n", k);

break;

case "F": {

double c = (t - 32) * 5 / 9;

double k = c + 273.15;

System.out.printf("Celsius: %.2f°C\n", c);

System.out.printf("Kelvin: %.2fK\n", k);

break;}

case "K": {

double c = t - 273.15;

double f = (c * 9 / 5) + 32;

System.out.printf("Celsius: %.2f°C\n", c);

System.out.printf("Fahrenheit: %.2f°F\n", f);

break;}

default:

System.out.println("Unknown unit! Please enter C, F, or K.");}

input.close(); }

You might also like