Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Convert Fahrenheit to Celsius
Temperature conversion between Fahrenheit and Celsius is a common programming task. In C#, you can easily convert Fahrenheit to Celsius using a simple mathematical formula and display the results with proper formatting.
The conversion formula subtracts 32 from the Fahrenheit temperature, then multiplies by 5/9 to get the equivalent Celsius temperature.
Formula
The mathematical formula for converting Fahrenheit to Celsius is −
celsius = (fahrenheit - 32) * 5 / 9
Using Direct Conversion
Here's a simple program that converts a fixed Fahrenheit temperature to Celsius −
using System;
class TemperatureConverter {
static void Main(string[] args) {
double fahrenheit = 97;
double celsius;
Console.WriteLine("Fahrenheit: " + fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
Console.WriteLine("Celsius: " + celsius.ToString("F2"));
}
}
The output of the above code is −
Fahrenheit: 97 Celsius: 36.11
Using a Method for Conversion
For better code organization, you can create a dedicated method to handle the conversion −
using System;
class TemperatureConverter {
static double FahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
static void Main(string[] args) {
double[] temperatures = { 32, 68, 98.6, 100, 212 };
Console.WriteLine("Fahrenheit\tCelsius");
Console.WriteLine("------------------------");
foreach (double temp in temperatures) {
double celsius = FahrenheitToCelsius(temp);
Console.WriteLine($"{temp}°F\t\t{celsius:F1}°C");
}
}
}
The output of the above code is −
Fahrenheit Celsius ------------------------ 32°F 0.0°C 68°F 20.0°C 98.6°F 37.0°C 100°F 37.8°C 212°F 100.0°C
Interactive Temperature Converter
This example demonstrates conversion with multiple temperature values and formatted output −
using System;
class Program {
static void Main() {
string[] descriptions = { "Freezing point", "Room temperature", "Body temperature", "Boiling point" };
double[] fahrenheitValues = { 32, 68, 98.6, 212 };
Console.WriteLine("Temperature Conversion Table");
Console.WriteLine("============================");
for (int i = 0; i < fahrenheitValues.Length; i++) {
double fahrenheit = fahrenheitValues[i];
double celsius = (fahrenheit - 32) * 5 / 9;
Console.WriteLine($"{descriptions[i]}: {fahrenheit}°F = {celsius:F1}°C");
}
}
}
The output of the above code is −
Temperature Conversion Table ============================ Freezing point: 32°F = 0.0°C Room temperature: 68°F = 20.0°C Body temperature: 98.6°F = 37.0°C Boiling point: 212°F = 100.0°C
Key Points
-
The conversion formula is
(fahrenheit - 32) * 5 / 9 -
Use
ToString("F2")or string interpolation with:F2to format decimal places -
Creating a separate method makes the code reusable and more organized
-
Always ensure proper data types − use
doublefor precise decimal calculations
Conclusion
Converting Fahrenheit to Celsius in C# is straightforward using the formula (fahrenheit - 32) * 5 / 9. You can implement this as a direct calculation or wrap it in a method for reusability, and use string formatting to control the display precision of the results.
