Wednesday, May 18, 2016

C# Program to Perform daily life Msurement Conversion


Program to Perform daily life Msurement 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 to convert : ");
num = Convert.ToInt32(System.Console.RdLine());
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.RdLine());
switch (ch)
{
case 'I':
{
f = (float)num * (float)2.5400;
Console.WriteLine("\n\t\t{0} inches = {1} cm\n\n", num, f);
brk;
}
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.RdLine());
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"); }
brk;
}
case 'M':
{
f = (float)num * (float)1.609344;
Console.WriteLine("\n\t\t{0} miles = {1} Kilometers\n\n", num , f);
brk;
}
case 'P':
{
f = (float)num / (float)2.2046;
Console.WriteLine("\n\t\t{0} lbs = {1} Kilograms\n\n", num, f );
brk;
}
default:
{
Console.WriteLine("\n\t\tInvalid input plse try again!\n\n" );
brk;
}
}
}

}

No comments:

Post a Comment