Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,950 questions

51,892 answers

573 users

How to convert between Fahrenheit, Celsius, and Kelvin in C#

1 Answer

0 votes
using System;

public class TemperatureConvert_CSharp
{
	public static void temperatureConvert(double temperature, char unitType, double[] results) {
		switch (unitType)
		{
			case 'c':
				results[0] = ((9.0 / 5.0) * temperature) + 32; // Fahrenheit
				results[1] = (temperature + 273.15); // Kelvin
				break;
			case 'f':
				results[0] = (temperature - 32) / 1.8; // Celsius
				results[1] = (temperature - 32) * 5 / 9.0 + 273.15; // Kelvin
				break;
			case 'k':
				results[0] = (temperature - 273.15); // Celsius
				results[1] = (((9.0 / 5.0) * temperature) - 459.67); // Fahrenheit
				break;
			default:
				results[0] = results[1] = 0;
				break;
		}
	}

	public static void Main(string[] args)
	{
		double[] result = new double[2];

		temperatureConvert(24, 'c', result);
		Console.WriteLine("Fahrenheit is: " + result[0] + " degrees\n" + 
		                  "Kelvin     is: " + result[1] + " degrees\n");

		temperatureConvert(3, 'f', result);
		Console.WriteLine("Celsius is: " + result[0] + " degrees\n" + 
		                  "Kelvin  is: " + result[1] + " degrees\n");

		temperatureConvert(3, 'k', result);
		Console.WriteLine("Celsius    is: " + result[0] + " degrees\n" + 
		                  "Fahrenheit is: " + result[1] + " degrees\n");
	}
}



/*
run:
    
Fahrenheit is: 75.2 degrees
Kelvin     is: 297.15 degrees

Celsius is: -16.1111111111111 degrees
Kelvin  is: 257.038888888889 degrees

Celsius    is: -270.15 degrees
Fahrenheit is: -454.27 degrees

*/

 



answered Dec 15, 2024 by avibootz

Related questions

1 answer 86 views
1 answer 96 views
1 answer 85 views
1 answer 87 views
1 answer 91 views
1 answer 84 views
1 answer 83 views
...