File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments