Advertisement
maus234

Converts Celsius to Fahrenheit

Jan 19th, 2024
1,194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Test1._1
  8. {
  9.  
  10.     class TemperatureConverter
  11.     {
  12.         // Converts Celsius to Fahrenheit
  13.         public static double CelsiusToFahrenheit(double celsius)
  14.         {
  15.             return celsius * 9 / 5 + 32; // Linear transformation from Celsius to Fahrenheit
  16.         }
  17.         public static double FahrenheitToCelsius (double fahrenheit)
  18.         {
  19.             return (fahrenheit - 32) * 5 / 9;  // Linear transformation from Fahrenheit to Celsius
  20.  
  21.         }
  22.  
  23.  
  24.     }
  25.     class Program
  26.     {
  27.         static void Main(string[] args)
  28.         {
  29.             // Test data
  30.             double[] testCelsius = new double[] { 0, 100, -40 };
  31.             double[] testFahrenheit = new double[] { 32, 212, -40 };
  32.  
  33.             // Test Celsius to Fahrenheit conversion
  34.             foreach (double temp in testCelsius)
  35.             {
  36.                 Console.WriteLine($"{temp} degrees Celsius is equal to {TemperatureConverter.CelsiusToFahrenheit(temp)} degrees Fahrenheit");
  37.             }
  38.  
  39.             // Test Fahrenheit to Celsius conversion
  40.             foreach (double temp in testFahrenheit)
  41.             {
  42.                 Console.WriteLine($"{temp} degrees Fahrenheit is equal to {TemperatureConverter.FahrenheitToCelsius(temp)} degrees Celsius");
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement