Java Program to Convert Celsius to Fahrenheit

Conversion from Celsius to Fahrenheit and from Fahrenheit to Celsius has an important role in the conversion of units system. In this post, we will develop a Java program to convert Celsius to Fahrenheit.

The formula used to convert from Celsius to Fahrenheit is given as, ⁰F= (⁰C * 9/5) + 32

Java Program to Convert Celsius to Fahrenheit

import java.util.Scanner;

public class TemperatureConversion {
  // method to convert celsius to fahrenheit
  private static double tofahrenheit(double celsius){
     return ( celsius * (9.0/5.0) + 32 );
  }

  public static void main(String[] args) {

     // declare variables
     double celsius = 0.0;
     double fahrenheit = 0.0;

     // create Scanner class object to read input
     Scanner scan = new Scanner(System.in);

     // read input
     System.out.print("Enter Celsius value:: ");
     celsius = scan.nextDouble();

     // convert temperature
     fahrenheit = tofahrenheit(celsius);

     // display result
     System.out.println("Fahrenheit value = "
                         +fahrenheit);

     // close Scanner class object
     scan.close();
  }
}

The output for different test-cases:-

Enter Celsius value:: 15
Fahrenheit value = 59.0

Enter Celsius value:: -40
Fahrenheit value = -40.0

Note:- The -40⁰C is equal to the -40⁰F.

Also See:-

Java Program to convert Celsius to Fahrenheit In a range

Generally, we need to convert Celsius to Fahrenheit in between a range. So, now we will Convert Celsius to Fahrenheit in between two numbers like for Celsius = 0,20,40,60,….,200. There are various ways to do this like using while loop or using for loop, or do-while loop.

import java.util.Scanner;

public class TemperatureConversionInRange {
  // method to convert celsius to fahrenheit
  public static double tofahrenheit(double celsius){
     return ( celsius * (9.0/5.0) + 32 );
  }

  public static void main(String[] args) {
     // declare variables
     int lower, upper, step;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read input
     System.out.print("Enter Lower limit:: ");
     lower = scan.nextInt();
     System.out.print("Enter Upper limit:: ");
     upper = scan.nextInt();
     System.out.print("Enter step:: ");
     step = scan.nextInt();

     // convert temperature
     System.out.println("Celsius \t"+
                        "Fahrenheit");

     for(int i=lower; i<=upper; i+=step)
        // display result
        System.out.println( i + "\t\t"
                       + tofahrenheit(i) );

     // close Scanner class object
     scan.close();
  }
}

The output for different test-cases:-

Enter Lower limit:: 0
Enter Upper limit:: 200
Enter step:: 20
Celsius Fahrenheit
0 32.0
20 68.0
40 104.0
60 140.0
80 176.0
100 212.0
120 248.0
140 284.0
160 320.0
180 356.0
200 392.0

Enter Lower limit:: -50
Enter Upper limit:: 50
Enter step:: 10
Celsius Fahrenheit
-50 -58.0
-40 -40.0
-30 -22.0
-20 -4.0
-10 14.0
0 32.0
10 50.0
20 68.0
30 86.0
40 104.0
50 122.0

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply