Java program to convert celsius to fahrenheit


How to convert celsius to fahrenheit in java

This Java program is about the temperature conversion from Celsius to Fahrenheit.
The program asks the user to enter a temperature in celsius and displays the temperature in fahrenheit.
The formula to convert celsius into fahrenheit:
F = 32 + ( 9 * C / 5)     
Java program for temperature conversion. Celsius into fahrenheit:
import java.util.Scanner;

public class CelsiusIntoFahrenheit {

      public static void main(String[] args) {
           Scanner sc = new Scanner(System.in);
           double celsius, fahrenheit;
           System.out.print("Enter temperature in Celsius: ");
           celsius = sc.nextDouble();
           fahrenheit = 32 + (celsius * 9 / 5);
           System.out.println(celsius +" ºC is equal to " + fahrenheit + " ºF");                                  
     }
}


Formula to convert from Fahrenheit to Celsius:
C = (F - 32) * 5 / 9     
Java program to convert Fahrenheit into Celsius:
import java.util.Scanner;
public class FahrenheitIntoCelsius {
     public static void main(String[] args) {
           Scanner sc = new Scanner(System.in);
           double celsius, fahrenheit;
           System.out.print("Enter temperature in Fahrenheit: ");
           fahrenheit = sc.nextDouble();
           celsius = (fahrenheit - 32) * 5 / 9;
           System.out.println(fahrenheit +" ºF is equal to " + celsius + " ºC");                                            
     }
}
This is a very simple program but it’s usefull to demonstrate input with Scanner Class and output with System.out.println
Here is a temperature conversion table from -5 celsius to 5 celsius:

Celsius
Fahrenheit
-5
23
-4
24.8
-3
26.6
-2
28.4
-1
30.2
0
32
1
33.8
2
35.6
3
37.4
4
39.2
5
41

And finally, here is a Java program to obtain a conversion table similar to this one from -5 celsius to +5 celsius. There is no input. We use now a for loop with initial value -5.
public class TemperatureConversionTable {
     public static void main(String[] args) {
        double fahrenheit;
        for (int celsius = -5; celsius <= 5; celsius++) {
            fahrenheit = 32 + (celsius * 9 / 5);
            System.out.println(celsius + " ºC => " + fahrenheit + " ºF");                                          
        }
    }
}
Output:
-5 ºC => 23.0 ºF   
-4 ºC => 25.0 ºF
-3 ºC => 27.0 ºF
-2 ºC => 29.0 ºF
-1 ºC => 31.0 ºF
0 ºC => 32.0 ºF
1 ºC => 33.0 ºF
2 ºC => 35.0 ºF
3 ºC => 37.0 ºF
4 ºC => 39.0 ºF
5 ºC => 41.0 ºF

5 comments :

  1. This comment has been removed by the author.

    ReplyDelete
  2. This is so helpful
    This question is in our book,it is related to If-else statement questions!!

    ReplyDelete
  3. This program is surely helpful a lot. But please also tell how to write a code for termination of this program when the user enters a non-numeric character. Thanks for your help with half of it. Please also tell further.
    Q no. 1 chapter 11(more basic on input/output Scanner and printer class)
    book- Understanding computer applications with blue j
    authors- vijay kumar pandey
    dilip kumar dey
    publication-apc

    ReplyDelete
  4. Great things you’ve always shared with us. Just keep writing this kind of posts.The time which was wasted in traveling for tuition now it can be used for studies.Thanks Crypto wallet insurance

    ReplyDelete