Computer Science, asked by blacktan, 1 month ago

Write a Java program to accept the temperature of any 5 cities in degrees Farenheit. Convert temperature to degree Centigrade using the given formula: Centigrade = (Farenheit - 32) * 5/9. Display the information in the given format. Also at the end print the total number of cities where the temperature in more than 35 degree Centigrade and the city name with maximum temperature. City Name ... * Farenheit temperature Centigrade temperature . Number of cities with temperature greater than 35 degree Centigrade: Name of the city with maximum temperature: Centigrade temperature​

Answers

Answered by saiyedsidra0783
3

Answer:

Java Program Convert Fahrenheit To Celsius | Vice Versa

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.

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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:

1

2

3

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:

1

2

3

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.prin

Similar questions