Temperature conversion between Celsius and Fahrenheit is a commonly used calculation in various fields including science and engineering. In this article, we will learn how to convert Celsius to Fahrenheit in C# and also how to convert Fahrenheit to Celsius using C# programming language.
Table of Contents
Celsius to Fahrenheit Conversion Formula
The formula for converting Celsius to Fahrenheit is straightforward:F = C * 9/5 + 32
where F is the temperature in Fahrenheit and C is the temperature in Celsius.
Code Example: Celsius to Fahrenheit Conversion
The following is a code example that demonstrates how to convert Celsius to Fahrenheit in C#.
using System;
namespace CelsiusToFahrenheitConversion
{
class Program
{
static void Main(string[] args)
{
double celsius;
double fahrenheit;
Console.WriteLine("Enter the temperature in Celsius: ");
celsius = Convert.ToDouble(Console.ReadLine());
fahrenheit = celsius * 9 / 5 + 32;
Console.WriteLine("Temperature in Fahrenheit: " + fahrenheit);
Console.ReadLine();
}
}
}
Output:
Code Explanation:
In the above code, we first declare two variables celsius and fahrenheit to store the temperature in Celsius and Fahrenheit respectively. Then, we use the Console.ReadLine()
method to read the temperature in Celsius from the user.
Next, we use the conversion formula to convert the temperature from Celsius to Fahrenheit:
fahrenheit = celsius * 9 / 5 + 32. Finally, we display the temperature in Fahrenheit using the Console.WriteLine()
method.
Fahrenheit to Celsius Conversion Formula
The formula for converting Fahrenheit to Celsius is:C = (F - 32) * 5/9
where F is the temperature in Fahrenheit and C is the temperature in Celsius.
Code Example: Fahrenheit to Celsius Conversion
The following is a code example that demonstrates how to convert Fahrenheit to Celsius in C#.
using System;
namespace FahrenheitToCelsiusConversion
{
class Program
{
static void Main(string[] args)
{
double fahrenheit;
double celsius;
Console.WriteLine("Enter the temperature in Fahrenheit: ");
fahrenheit = Convert.ToDouble(Console.ReadLine());
celsius = (fahrenheit - 32) * 5 / 9;
Console.WriteLine("Temperature in Celsius: " + celsius);
Console.ReadLine();
}
}
}
Output:
Enter the temperature in Fahrenheit:
95
Temperature in Celsius: 35
Code Explanation:
In the above code, we first declare two variables fahrenheit
and celsius
to store the temperature in Fahrenheit and Celsius respectively. Then, we use the Console.ReadLine()
method to read the temperature in Fahrenheit from the user.
Next, we use the conversion formula to convert the temperature from Fahrenheit to Celsius:
celsius = (fahrenheit – 32) * 5 / 9. Finally, we display the temperature in Celsius using the Console.WriteLine()
method.
What is 180 Celsius in Fahrenheit?
To convert 180 Celsius to Fahrenheit, we can use the conversion formula:F = C * 9/5 + 32
Where F is the temperature in Fahrenheit and C is the temperature in Celsius. Putting the value 180 for C, we will get:
F = 180 * 9/5 + 32
F = 324
So, 180 Celsius is equal to 324 Fahrenheit.
Conclusion:
In this article, we have covered the basics of converting temperatures between Celsius and Fahrenheit in C#. We have learned the conversion formulas and provided code examples and explanations for both Celsius to Fahrenheit and Fahrenheit to Celsius conversions. With this knowledge, you should be able to easily implement temperature conversions in your C# projects.
FAQs
Q: What is the formula to convert Celsius to Fahrenheit in C#?
The formula to convert Celsius to Fahrenheit is F = C * 9/5 + 32, where F is the temperature in Fahrenheit and C is the temperature in Celsius.
Q: How to convert Fahrenheit to Celsius in C#?
To convert Fahrenheit to Celsius in C#, We can use the formula C = (F – 32) * 5/9, where F is the temperature in Fahrenheit and C is the temperature in Celsius. We have provided a code example and explanation in this article for converting Fahrenheit to Celsius in C#.
Q: Can you convert temperatures in other programming languages besides C#?
Yes, the formulas for converting temperatures between Celsius and Fahrenheit are the same in any programming language. You can implement these formulas in any programming language of your choice.
Q: Why is it important to know how to convert temperatures in programming?
Converting temperatures is a commonly used calculation in many fields. Knowing how to convert temperatures in programming can be useful for various projects and applications that require temperature conversions. Additionally, it helps improve your programming skills and knowledge.
References: Tutorialspoint- Celsius to Fahrenheit
Articles you might also like:
- Program to Convert Fahrenheit to Celsius: Algorithm, Formula, and Code Examples
- Fibonacci sequence: Fibonacci series in C# (with examples)
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- C# Abstract class Vs Interface
- C# Array vs List: When should you use an array or a List?
- Generic Delegates in C# With Examples
- Properties In C# with examples
- Multithreading in C#
- Difference Between if-else and switch
- Local vs Global Variables
- C# stack vs heap
- Readonly vs const in C#
- Fields vs Properties In C#
- Abstract Factory Design Pattern in C#: Real-World Example and Code Explanations
- Singleton Design Pattern in C#: A Beginner’s Guide with Examples
- SOLID Design Principles in C#: A Complete Example
We would love to hear your thoughts on this post. Please leave a comment below and share it with others.
- Difference Between Array And ArrayList In C#: Choosing the Right Collection - May 28, 2024
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024