Program to Convert Fahrenheit to Celsius: Algorithm, Formula, and Code Examples

If you’ve ever had to convert temperature readings from Fahrenheit to Celsius or vice versa, you know how important it is to have a reliable program to do this.

This article will discuss the formula and algorithm for converting Fahrenheit to Celsius and provide complete code examples in C#, Python, C, Java, and PHP.

program-to-convert-fahrenheit-to-celsius
Fahrenheit to Celsius

Formula: Fahrenheit to Celsius

To convert Fahrenheit to Celsius, we use the following formula:

C = (F - 32) x 5/9

Where:

  • C is the temperature in Celsius
  • F is the temperature in Fahrenheit

Algorithm: Fahrenheit to Celsius

Here is the algorithm we will use to convert Fahrenheit to Celsius:

  1. Start
  2. Read the Fahrenheit temperature value
  3. Subtract 32 from the Fahrenheit temperature value
  4. Multiply the result by 5/9
  5. Store the result in the Celsius temperature variable
  6. Display the Celsius temperature value
  7. Stop

Now let’s move on to the complete code examples for converting Fahrenheit to Celsius in different programming languages.

Program to Convert Fahrenheit into Celsius in C#

Here is the C# code for converting Fahrenheit to Celsius:

using System;

class FahrenheitToCelsius
{
    static void Main(string[] args)
    {
        double fahrenheit, celsius;
        Console.Write("Enter temperature in Fahrenheit: ");
        fahrenheit = Convert.ToDouble(Console.ReadLine());
        celsius = (fahrenheit - 32) * 5 / 9;
        Console.WriteLine("Temperature in Celsius: {0}", celsius);
    }
}
Program to convert Fahrenheit into Celsius
Program to convert Fahrenheit into Celsius in C#

Output:

Enter temperature in Fahrenheit: 54
Temperature in Celsius: 12.2222222222222

Program to Convert Fahrenheit into Celsius in Python

Here is the Python code for converting Fahrenheit to Celsius:

fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5 / 9
print("Temperature in Celsius: ", celsius)

Output:

Enter temperature in Fahrenheit: 77
Temperature in Celsius: 25.0

Program to Convert Fahrenheit into Celsius in C

Here is the program code for converting Fahrenheit to Celsius:

#include <stdio.h>

int main()
{
    float fahrenheit, celsius;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);
    celsius = (fahrenheit - 32) * 5 / 9;
    printf("Temperature in Celsius: %f", celsius);
    return 0;
}

Output:

Enter temperature in Fahrenheit: 77
Temperature in Celsius: 25.000000

Program to Convert Fahrenheit into Celsius in Java

Here is the Java program code for converting Fahrenheit to Celsius:

import java.util.Scanner;

public class FahrenheitToCelsius {
    public static void main(String[] args) {
        double fahrenheit, celsius;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter temperature in Fahrenheit: ");
        fahrenheit = input.nextDouble();
        celsius = (fahrenheit - 32) * 5 / 9;
        System.out.println("Temperature in Celsius: " + celsius);
    }
}

Output:

Enter temperature in Fahrenheit: 77
Temperature in Celsius: 25.0

Program to Convert Fahrenheit into Celsius in PHP

To convert Fahrenheit to Celsius in PHP, you can use the following code:

<?php
$fahrenheit = 77;
$celsius = ($fahrenheit - 32) * 5 / 9;
echo "Temperature in Celsius: " . $celsius;
?>

This program first initializes a variable $fahrenheit to a value of 77.

It then uses the formula ($fahrenheit - 32) * 5 / 9 to convert this temperature reading from Fahrenheit to Celsius.
Finally, it outputs the temperature in Celsius using the echo statement.

Output:

Temperature in Celsius: 25

FAQs:

Q: Why do we use the formula C = (F – 32) x 5/9 to convert Fahrenheit to Celsius?

This formula is based on the freezing and boiling points of water. At sea level, water freezes at 32 degrees Fahrenheit and boils at 212 degrees Fahrenheit. In Celsius, water freezes at 0 degrees and boils at 100 degrees.

By subtracting 32 from the Fahrenheit temperature and then multiplying by 5/9, we can convert Fahrenheit to Celsius.

Q: What is the difference between Fahrenheit and Celsius?

Fahrenheit and Celsius are both units of temperature measurement. Fahrenheit is used primarily in the United States, while Celsius is used in most other countries. The main difference between the two scales is their zero points.

In Fahrenheit, the zero point is -459.67 degrees, which is the lowest possible temperature. In Celsius, the zero point is 0 degrees, which is the freezing point of water.

Q: Can you convert Celsius to Fahrenheit using the same formula?

Yes, you can use the following formula to convert Celsius to Fahrenheit:
F = (C x 9/5) + 32, Where F is the temperature in Fahrenheit and C is the temperature in Celsius.

Conclusion

Converting temperature readings from Fahrenheit to Celsius (or Celsius to Fahrenheit ) is a common task in many applications.

In this article, we have provided the formula and algorithm for converting Fahrenheit to Celsius, and given complete code examples in C#, Python, C, Java, and PHP.

References: JavaTpoint-Fahrenheit into Celsius

Articles you might also like:

Shekh Ali
4 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments