Mon Sep 20 2021
Temperature Converter
Java Programming1474 views
File Name: temperature-converter.java
import java.io.*;
import java.util.Scanner;
class temperature {
/* Global variables */
double cel,fer;
temperature(int t_type,double temp) {
switch (t_type) {
case 1:
cel_fer(temp);
break;
case 2:
fer_cel(temp);
break;
default:
System.out.println("Invalid Choice!");
}
}
void cel_fer(double cel) {
fer = ((cel * 9) / 5) + 32;
System.out.println("Fahrenheit: "+fer);
}
void fer_cel(double fer) {
cel = ((fer - 32) *5) / 9;
System.out.println("Celsius: "+cel);
}
}
class converter {
public static void main(String args[ ]) {
int opt;
double temp;
Scanner input = new Scanner(System.in);
System.out.println("1 : Celsius to Fahrenheit \n2 : Fahrenheit to Celsius");
System.out.println("Enter your choice:");
opt = Integer.parseInt(input.nextLine());
System.out.println("Enter the temperature value:");
temp = Double.parseDouble(input.nextLine());
new temperature(opt,temp);
}
}
/* Output */
1 : Celsius to Fahrenheit
2 : Fahrenheit to Celsius
Enter your choice:
1
Enter the temperature value:
0
Fahrenheit: 32.0
/* ----------------------------------- */
1 : Celsius to Fahrenheit
2 : Fahrenheit to Celsius
Enter your choice:
1
Enter the temperature value:
32
Fahrenheit: 0.0
Reference:
Celsius and Fahrenheit
Author:Geekboots