Home Programs How to convert between celsius and fahrenheit in C#

How to convert between celsius and fahrenheit in C#

In this article we will show you how to convert from Celsius to Fahrenheit and from Fahrenheit to Celsius

We use the following formulas

To convert from Celsius to Fahrenheit:

F = (C * 9) / 5 + 32;

To convert from Fahrenheit to Celsius:

C = (F – 32) * 5 / 9;

Where F = Fahrenheit and C = Celsius.

Example

Convert from celsius to fahrenheit

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            double fahrTemp, celsTemp;
            // get the user input
            Console.Write("Enter the temperature in Celsius:");
            celsTemp = Convert.ToDouble(Console.ReadLine());
            //convert using the formula
            fahrTemp = (celsTemp * 1.8) + 32;
            //display results
            Console.WriteLine("Celsius is {0} -> Fahrenheit is {1}", celsTemp, fahrTemp);

            Console.ReadLine();
        }
    }
}

Here is a test run

Enter the temperature in Celsius:20
Celsius is 20 -> Fahrenheit is 68

Convert from celsius to fahrenheit

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            double fahrTemp, celsTemp;
            // get the user input
            Console.Write("Enter the temperature in fahrenheit:");
            fahrTemp = Convert.ToDouble(Console.ReadLine());
            //convert using the formula
            celsTemp = (fahrTemp - 32) * 5 / 9;
            //display results
            Console.WriteLine("Celsius is {0} -> Fahrenheit is {1}", celsTemp, fahrTemp);

            Console.ReadLine();
        }
    }
}

Here is a test run

Enter the temperature in fahrenheit:68
Celsius is 20 -> Fahrenheit is 68

You may also like