C# Program to Convert Celsius to Fahrenheit
You can convert Celsius to Fahrenheit in C# using the formula
fahrenheit = (celsius * 9 / 5) + 32. For example, double fahrenheit = (celsius * 9.0 / 5.0) + 32; converts a Celsius value to Fahrenheit.Examples
Input0
Output32
Input25
Output77
Input-40
Output-40
How to Think About It
To convert Celsius to Fahrenheit, multiply the Celsius temperature by 9, then divide by 5, and finally add 32. This formula changes the scale from Celsius to Fahrenheit, which have different zero points and increments.
Algorithm
1
Get the temperature value in Celsius.2
Multiply the Celsius value by 9.3
Divide the result by 5.4
Add 32 to the result.5
Return or display the Fahrenheit temperature.Code
csharp
using System; class Program { static void Main() { Console.Write("Enter temperature in Celsius: "); double celsius = Convert.ToDouble(Console.ReadLine()); double fahrenheit = (celsius * 9.0 / 5.0) + 32; Console.WriteLine($"Temperature in Fahrenheit: {fahrenheit}"); } }
Output
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77
Dry Run
Let's trace converting 25 Celsius to Fahrenheit through the code
1
Input Celsius
User inputs 25
2
Multiply by 9
25 * 9 = 225
3
Divide by 5
225 / 5 = 45
4
Add 32
45 + 32 = 77
5
Output Fahrenheit
Print 77
| Step | Operation | Value |
|---|---|---|
| 1 | Input Celsius | 25 |
| 2 | Multiply by 9 | 225 |
| 3 | Divide by 5 | 45 |
| 4 | Add 32 | 77 |
| 5 | Output Fahrenheit | 77 |
Why This Works
Step 1: Multiply Celsius by 9
Multiplying by 9 scales the Celsius temperature to match the Fahrenheit scale increments.
Step 2: Divide by 5
Dividing by 5 adjusts the scale ratio between Celsius and Fahrenheit degrees.
Step 3: Add 32
Adding 32 shifts the zero point from Celsius to Fahrenheit, aligning freezing points.
Alternative Approaches
Using a function
csharp
using System; class Program { static double CelsiusToFahrenheit(double celsius) { return (celsius * 9.0 / 5.0) + 32; } static void Main() { Console.Write("Enter Celsius: "); double c = Convert.ToDouble(Console.ReadLine()); Console.WriteLine($"Fahrenheit: {CelsiusToFahrenheit(c)}"); } }
Separates conversion logic into a reusable function for cleaner code.
Using integer math (legacy)
csharp
using System; class Program { static void Main() { Console.Write("Enter Celsius: "); int celsius = Convert.ToInt32(Console.ReadLine()); int fahrenheit = (celsius * 9 / 5) + 32; Console.WriteLine($"Fahrenheit: {fahrenheit}"); } }
Uses integer math which may lose precision; not recommended for decimal temperatures.
Complexity: O(1) time, O(1) space
Time Complexity
The calculation uses a fixed number of arithmetic operations, so it runs in constant time.
Space Complexity
Only a few variables are used, so space usage is constant.
Which Approach is Fastest?
All approaches run in constant time; using a function improves code reuse but does not affect speed.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Inline calculation | O(1) | O(1) | Simple quick conversion |
| Function method | O(1) | O(1) | Reusable and clean code |
| Integer math | O(1) | O(1) | Legacy code, less precise |
Use double type for temperature to handle decimal values accurately.
Forgetting to use 9.0 and 5.0 as doubles causes integer division and wrong results.