Tuesday, November 29, 2016

C# Program to Perform daily life Measurement Conversion

Program to Perform daily life Measurement Conversion 

Program statement:
Write a program using switch statement which takes one value from user and ask about type of conversion and then will perform conversion according to type of conversion.if user enters
I -> convert from inches to centimeters.
G -> convert from gallons to liters.
M -> convert from mile to kilometer.
P -> convert from pound to kilogram.
If user enters any other character then show a proper message.

Solution:
public class con
{
int num; char ch;
float f;
public void coversion()
{
Console.Write("\n\t\tEnter a number to convert : ");
num = Convert.ToInt32(System.Console.ReadLine());
Console.WriteLine("\t\tI -> convert from inches to centimeters.");
Console.WriteLine("\t\tG -> convert from gallons to liters.");
Console.WriteLine("\t\tM -> convert from mile to kilometer.");
Console.WriteLine("\t\tP -> convert from pound to kilogram.");
Console.Write("\n\t\tSelect conversion you want to do: ");
ch = Convert.ToChar(System.Console.ReadLine());
switch (ch)
{
case 'I':
{
f = (float)num * (float)2.5400;
Console.WriteLine("\n\t\t{0} inches = {1} cm\n\n", num, f);
break;
}
case 'G':
{
char ch2;
Console.WriteLine("\t\tI=> imperial gallon");
Console.WriteLine("\t\tU=> US gallon");
Console.Write("\n\t\tWhich one you want to enter : ");
ch2 = Convert.ToChar(Console.ReadLine());
if (ch2 == 'I')
{
f = (float)num * (float)4.546;
Console.WriteLine("\n\t\t{0} imperial gallons = {1} litr es\n\n", num, f);
}
else if (ch2 == 'U')
{
f = (float)num * (float)3.785;
Console.WriteLine("\n\t\t{0} US gallons = {1} litres\n\n" , num, f);
}
else
{ Console.WriteLine("\t\tInvalid input!\n\n"); }
break;
}
case 'M':
{
f = (float)num * (float)1.609344;
Console.WriteLine("\n\t\t{0} miles = {1} Kilometers\n\n", num , f);
break;
}
case 'P':
{
f = (float)num / (float)2.2046;
Console.WriteLine("\n\t\t{0} lbs = {1} Kilograms\n\n", num, f );
break;
}
default:
{
Console.WriteLine("\n\t\tInvalid input please try again!\n\n" );
break;
}
}
}

}