Java Write a Program That Asks Converts Centigrade Temperature to Fahrenheit

Java Program to convert Fahrenheit to Celsius – Here we discuss the various methods to convert the Fahrenheit temperature measurement to Celsius temperature measurement and vice versa. The various methods include Static Method, Switch Case and the method. We have added the compiler to each case with sample outputs citing specific examples.

  • Hollow Mirrored Right Triangle Star Pattern
  • Fibonacci Series In Java Program – 4 Multiple Ways

The following program has been written in 4 Possible Ways:

  • Static Method
  • Using Method
  • Fahrenheit to Celsius and Vice Versa Using Switch Case
  • Celsius To Fahrenheit

Celsius Temperature Scale: Earlier known as the Centigrade Scale, the Celsius Scale is a widely used one, also an SI derived unit for temperature. The normal scale of a Celsius thermometer measures from 0°C (Water's freezing point at Standard Atmospheric Pressure) to 100°C (Water's boiling point at Standard Atmospheric Pressure)

Fahrenheit Temperature Scale: The Fahrenheit Scale is specifically used in the U.S.A and few other places. The normal scale of a Fahrenheit thermometer ranges from 32°F (Water's freezing point at Std Atm Pressure) and 212°F (Water's boiling point at Std Atm Pressure)

The formula to convert Fahrenheit into Celsius:

Java Program Fahrenheit to Celsius

The formula to convert Celsius to Fahrenheit:

Java Program Celsius to Fahrenheit

Using Static Method

1) Read the temperature value using scanner object as sc.nextInt() and store it in the variable a.

In this program, we have the static method celsius(double f), which converts the Fahrenheit temperature into Celsius temperature by using the formula (f-32)*5/9.

2) In the main method, call the celsius method as celsius(a), then celsius(double f) method returns the Celsius temperature.

import java . util . Scanner ;

class FahrenheittoCelsius

{

public static void main ( String arg [ ] )

{

double a , c ;

Scanner sc = new Scanner ( System . in ) ;

System . out . println ( "Enter  Fahrenheit temperature" ) ;

a = sc . nextDouble ( ) ;

System . out . println ( "Celsius temperature is = " + celsius ( a ) ) ;

}

static double celsius ( double f )

{

return ( f - 32 ) * 5 / 9 ;

}

}

Output:

Enter Fahrenheit temperature

56

Celsius temperature is = 13.333333333333334

Fahrenheit to Celsius Using method

1) In this program celsius(double f) is the method which calculates the Celsius temperature to the given Fahrenheit temperature using the formula (f-32)*5/9.

2) We will call this method using  FahrenheittoCelsius class object "fah" as fah.celsius(a), this method returns the double data type value.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

import java . util . Scanner ;

class FahrenheittoCelsius

{

double celsius ( double f )

{

return ( f - 32 ) * 5 / 9 ;

}

public static void main ( String arg [ ] )

{

double a , c ;

Scanner sc = new Scanner ( System . in ) ;

System . out . println ( "Enter  Fahrenheit temperature" ) ;

a = sc . nextDouble ( ) ;

FahrenheittoCelsius fah = new FahrenheittoCelsius ( ) ;

double result = fah . celsius ( a ) ;

System . out . println ( "Celsius temperature is = " + result ) ;

}

}

Output:

Enter Fahrenheit temperature

250

Celsius temperature is = 121.11111111111111

Fahrenheit to Celsius and Vice Versa Using Switch Case

1) Read the value using scanner class object sc.nextInt(), and store it in the variable ch. Here ch represents the  type of conversion i.e ch=1 represents  Fahrenheit to Celsius,ch=2 represents  Celsius  to Fahrenheit

2) If ch matches with case 1 then that block will be executed, if ch=2 then case 2 blocks will be executed. If we don't have a break-in case then switch executes all cases.

3) If none of the cases matches with the switch then default block will be executed.

4) In our example ch=1 so case 1: will be executed and calculates the Celsius temperature using the formulas

c=(f-32)*5/9. it has "break" so it comes out of the switch.

if ch=2, then switch search for the case which matches with ch=2, so case 1 doesn't match so come to case 2 and executes the case 2 block. Here case 1 will not be executed.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

import java . util . Scanner ;

class FC

{

public static void main ( String arg [ ] )

{

double f , c ;

Scanner sc = new Scanner ( System . in ) ;

System . out . println ( "Choose type of conversion \n 1.Fahrenheit to Celsius  \n 2.Celsius to Fahrenheit" ) ;

int ch = sc . nextInt ( ) ;

switch ( ch )

{

case 1 : System . out . println ( "Enter  Fahrenheit temperature" ) ;

f = sc . nextDouble ( ) ;

c = ( f - 32 ) * 5 / 9 ;

System . out . println ( "Celsius temperature is = " + c ) ;

break ;

case 2 : System . out . println ( "Enter  Celsius temperature" ) ;

c = sc . nextDouble ( ) ;

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

System . out . println ( "Fahrenheit temperature is = " + f ) ;

break ;

default : System . out . println ( "please choose valid choice" ) ;

}

}

}

Output:

Choose type of conversion

1.Fahrenheit to Celsius

2.Celsius to Fahrenheit

1

Enter Fahrenheit temperature

32

Celsius temperature is = 0.0

Choose type of conversion

1.Fahrenheit to Celsius

2.Celsius to Fahrenheit

2

Enter Celsius temperature

32

Fahrenheit temperature is = 89.6

C To F using Static Method

1) In this program, we have the static method fahrenheit(double c) which converts the Celsius temperature into Fahrenheit temperature using the formula  ((9*c)/5)+32.

2) The main method which is static calls the static method fahrenheit(double c) as fahrenheit(a) then it returns the Fahrenheit temperature.

import java . util . Scanner ;

class CelsiustoFahrenheit

{

public static void main ( String arg [ ] )

{

double a , c ;

Scanner sc = new Scanner ( System . in ) ;

System . out . println ( "Enter  Celsius temperature" ) ;

a = sc . nextDouble ( ) ;

System . out . println ( "Fahrenheit temperature is = " + fahrenheit ( a ) ) ;

}

static double fahrenheit ( double c )

{

return ( ( 9 * c ) / 5 ) + 32 ;

}

}

Output:

Enter Celsius temperature

32

Fahrenheit temperature is = 89.6

C To F using Method

1) double fahrenheit(double c) is the method which calculates fahrenheit temperature to the given celsius temperature using the formula  ((9*c)/5)+32.

2) We will call this method using CelsiustoFahrenheit class object "cel" as cel.fahrenheit(a), then it returns the value, and assigned to the variable "result".

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

import java . util . Scanner ;

class CelsiustoFahrenheit

{

double fahrenheit ( double c )

{

return ( ( 9 * c ) / 5 ) + 32 ;

}

public static void main ( String arg [ ] )

{

double a , c ;

Scanner sc = new Scanner ( System . in ) ;

System . out . println ( "Enter  Celsius temperature" ) ;

a = sc . nextDouble ( ) ;

CelsiustoFahrenheit cel = new CelsiustoFahrenheit ( ) ;

double result = cel . fahrenheit ( a ) ;

System . out . println ( "Fahrenheit temperature is = " + result ) ;

}

}

Java Write a Program That Asks Converts Centigrade Temperature to Fahrenheit

Source: https://javatutoring.com/java-convert-fahrenheit-to-celsius/

0 Response to "Java Write a Program That Asks Converts Centigrade Temperature to Fahrenheit"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel