Formula #11 (Convert Celsius to Fahrenheit
and vice-versa)
(a) Convert Celsius to Fahrenheit (b)
Convert Fahrenheit to Celsius
Enter your selection a/b: b
Enter temperature in Fahrenheit: 100.4
Temperature in Celsius: 38
The program for the formula is written
in C# programming language and will accept a character as a case selection.
Case A) will help you to Convert Celsius
to Fahrenheit.
Case B) will help you to Convert Fahrenheit
to Celsius. Based on the formula it will find out the output.
Let’s find out a simple and easy way
to code these formula.
Practical Implementation:
using System;
namespace patternProblem.Formula
{
class Formula11 //find student grade
{
static void Main(string[] args)
{
Console.Write("(a) Convert Celsius to Fahrenheit (b) Convert Fahrenheit
to Celsius \nEnter your selection a/b: ");
string choice = Console.ReadLine().ToLower();
double cel = 0.0, far = 0.0; ;
switch (choice)
{
case "a":
Console.Write("Enter temperature in Celsius:
");
cel = Convert.ToDouble(Console.ReadLine());
far = cel * 9 / 5 + 32;
Console.WriteLine("Temperature in Fahrenheit:
" + far);
break;
case "b":
Console.Write("Enter temperature in Fahrenheit:
");
far = Convert.ToDouble(Console.ReadLine());
cel = (far - 32) * 5 / 9;
Console.WriteLine("Temperature in Celsius:
" + cel);
break;
default:
Console.WriteLine("Invalid !
Selection");
break;
}
Console.ReadKey();
}
}
}
Output:
The input case
will decide what you want to convert either the Fahrenheit to Celsius or
the Celsius to Fahrenheit.
So, based on the input case you’ll get the output. The output is shown below:
(a) Convert Celsius to Fahrenheit (b) Convert Fahrenheit
to Celsius
Enter your selection a/b: b
Enter temperature in Fahrenheit: 100.4
Temperature in Celsius: 38
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table – Formula
based program
Next – String based program
Keep learning and sharing...
No comments:
Post a Comment