Practical : 11
Subject : .NET
Aim : Write a C# code to Perform Celsius to Fahrenheit Conversion and Fahrenheit to Celsius conversion.
Code:
using System;
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Enter your Choice : ");
Console.WriteLine("1. Celsius to Fahrenheit");
Console.WriteLine("2. Fahrenheit to Celsius");
Console.WriteLine("3. Exit");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
C2F();
break;
case 2:
F2C();
break;
case 3:
break;
default:
Console.WriteLine("Enter valid choice");
break;
}
if (choice == 3)
break;
}
Console.ReadKey();
}
static void C2F()
{
Console.WriteLine("Enter temperature in Celsius:");
double c = double.Parse(Console.ReadLine());
double f = (c * 1.8) + 32;
Console.WriteLine("Celsius to Fahrenheit :\t{0}\n", f);
}
static void F2C()
{
Console.WriteLine("Enter temperature in Fahrenheit:");
double f = double.Parse(Console.ReadLine());
double c = (f - 32) / 1.8;
Console.WriteLine(" Fahrenheit to Celsius:\t{0}\n", c);
}
}
class Program
{
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Enter your Choice : ");
Console.WriteLine("1. Celsius to Fahrenheit");
Console.WriteLine("2. Fahrenheit to Celsius");
Console.WriteLine("3. Exit");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
C2F();
break;
case 2:
F2C();
break;
case 3:
break;
default:
Console.WriteLine("Enter valid choice");
break;
}
if (choice == 3)
break;
}
Console.ReadKey();
}
static void C2F()
{
Console.WriteLine("Enter temperature in Celsius:");
double c = double.Parse(Console.ReadLine());
double f = (c * 1.8) + 32;
Console.WriteLine("Celsius to Fahrenheit :\t{0}\n", f);
}
static void F2C()
{
Console.WriteLine("Enter temperature in Fahrenheit:");
double f = double.Parse(Console.ReadLine());
double c = (f - 32) / 1.8;
Console.WriteLine(" Fahrenheit to Celsius:\t{0}\n", c);
}
}
Output:
Sign up here with your email
ConversionConversion EmoticonEmoticon