Program toward Perform daily life Measurement Conversion
Program statement:
Write a program using switch statement which takes one value from user furthermore ask about type regarding conversion furthermore then will perform conversion according toward type regarding conversion.if user enters
I -> convert from inches toward centimeters.
G -> convert from gallons toward liters.
M -> convert from mile toward kilometer.
P -> convert from pound toward 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 toward convert : ");
num = Convert.ToInt32(System.Console.ReadLine());
Console.WriteLine("\t\tI -> convert from inches toward centimeters.");
Console.WriteLine("\t\tG -> convert from gallons toward liters.");
Console.WriteLine("\t\tM -> convert from mile toward kilometer.");
Console.WriteLine("\t\tP -> convert from pound toward kilogram.");
Console.Write("\n\t\tSelect conversion you want toward 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 toward 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;
}
}
}
}