In: Computer Science
Write a JAVA program that displays a table of the Celsius
temperatures and their Fahrenheit
equivalents.
The formula for converting a temperature from Celsius to
Fahrenheit is
F = 9/ 5 C + 32
where F → Fahrenheit temperature
C → Celsius temperature.
Allow the user to enter the range of temperatures in Celsius to
be converted into Fahrenheit.
Your program must use a loop to display the values of the
temperature conversions (see sample
output).
Sample Output:
Welcome to the Temperature Converter!! Enter the range of
temperatures in Celsius to be converted into
Fahrenheit
Enter minimum temperature in Celsius:
5
Enter maximum temperature in Celsius:
7
The corresponding Fahrenheit equivalents are:
5.0 C 23.0 F
4.0 C 24.8 F
3.0 C 26.6 F
2.0 C 28.4 F
1.0 C 30.2 F
0.0 C 32.0 F
1.0 C 33.8 F
2.0 C 35.6 F
3.0 C 37.4 F
4.0 C 39.2 F
5.0 C 41.0 F
6.0 C 42.8 F
7.0 C 44.6 F
Goodbye!
Algorithm to display Celsius temperatures and their Fahrenheit equivalents
1.Input mintemp from user.
2.Input maxterm from user.
3.Implement a for loop from i=minterm to maxterm
3.1) Calculate ((9*i)/5)+32 and store result in variable fah.
3.2) Print i,fah
Java program to implement above algorithm
import java.util.*;
public class Main
{
public static void main(String[] args) {
double mintemp,maxtemp,fah; //declaring variables
Scanner sc=new Scanner(System.in); //calling scanner class
System.out.println("Welcome to the Temperature Converter!! Enter the range of temperatures in Celsius to be converted into Fahrenheit");
System.out.println("Enter minimum temperature in Celsius:");
mintemp=sc.nextDouble(); //taking minimum temperature in Celsius
System.out.println("Enter maximum temperature in Celsius:");
maxtemp=sc.nextDouble(); //taking maximum temperature in Celsius
double i;
System.out.println("The corresponding Fahrenheit equivalents are");
for(i=mintemp;i<=maxtemp;i++) //implementing for loop for range of temperature
{
fah=((9*i)/5)+32; //converting temperature into Fahrenheit
System.out.println(i+"C "+fah+" F"); //printing Celsius temperature and converted Fahrenheit temperature
}
}
}
Output of above code is: