Skip to content

Commit 6cf3762

Browse files
Merge pull request #130 from MohammedHassan07/temp
Solution of the temperature conversion
2 parents ffa8d6c + c5d74e6 commit 6cf3762

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Basic Codes/TempConversion.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Scanner;
2+
3+
public class TempConversion {
4+
public static void main(String[] args) {
5+
// Question: Convert the temperature from celsius to fahrenheit and Fahrenheit to
6+
// Celsius according to the user`s choice provided by the input
7+
8+
System.out.println("To convert from Celsius to Fahrenheit Enter 0\n" + "To convert from Fahrenheit to Celsius enter 1"); // Asking user for input
9+
10+
Scanner input = new Scanner(System.in);
11+
int n = input.nextInt(); // Taking input to choose the temperature conversion
12+
13+
switch (n) {
14+
15+
case 0:
16+
System.out.println("Enter the temperature in Celsius");
17+
18+
float temp = input.nextFloat(); // Taking the temperature in celsius
19+
double ans = ((temp * 9) / 5) + 32; // Formula to calculate the temperature in celsius
20+
21+
System.out.printf("The %.3f degree celsius = %.3f Fahrenheit", temp, ans);
22+
break;
23+
24+
case 1:
25+
System.out.println("Enter the temperature in Fahrenheit");
26+
27+
temp = input.nextFloat(); // Taking the temperature in celsius
28+
ans = ((temp - 32) * 5) / 9; // Formula to calculate the temperature in celsius
29+
30+
System.out.printf("The %.3f = %.3f degree celsius", temp, ans);
31+
break;
32+
33+
default: // Showing the message if user provides invalid argument
34+
System.out.println("Enter valid input");
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)