Conversion Program: Write a program that perform conversions that we use often.
ID: 3750258 • Letter: C
Question
Conversion Program: Write a program that perform conversions that we use often. Program should display the following menu and ask the user to enter the choice. For example, if choice 1 is selected, then the program should ask the user to enter the Fahrenheit temperature and call the function double fahrenheitToCilsuis(double fTemp) to get the conversion. So, you need to implement following functions in your program. Welcome to the Conversion Program 1. Fahrenheit to Celsius Miles to kilometers 3. 2. Liters to Gallons 4. Exit from the program You should implement this program using functions and your program need to have following functions void displayMenu() double fahrenheitToCilsuis(double fTemp) double milesToKilometers(double miles) double litersToGallons(doube liters)Explanation / Answer
using System;
public class Program
{
public void DisplayMenu() // this finction will display conversion menu
{
Console.WriteLine(" Welcome To Conversion Program");
Console.WriteLine(" =============================");
Console.WriteLine("1. Fahrenhite to Celsius");
Console.WriteLine("2. Miles to Kilometers");
Console.WriteLine("3. Liters to Gallons");
Console.WriteLine("4. Exit from Program");
Console.WriteLine("Please enter your choice");
}
public string Choice() // in this function user will make his choice
{
string menuitem = Console.ReadLine();
return menuitem;
}
public void menu() // this function is calling displaymenu function and then calling one more function //in which if else conditions are their for choices
{
Program p=new Program();
p.DisplayMenu();
string choice=p.Choice();
p.menuitem(choice);
}
public double FahrenhiteToCelsius(double ftemp) // this will convert fahrenhite temperature to celsius
{
double celsius = (ftemp-32) /1.8;
return celsius;
}
public double milestoKilometers(double miles) // this will convert miles to km
{
double km= miles/0.6214;
return km;
}
public double litersToGallons(double liters) // this will convert liters to gallons
{
double gallon= liters*0.2642;
return gallon;
}
public void menuitem( string choice) // this contains if else condition for every choice and calling //the respective function
{
Program p1=new Program();
if(choice=="1")
{
Console.WriteLine("Enter the temperature in Fahrenhite");
double ftemp=Convert.ToDouble(Console.ReadLine());
double tempc=p1.FahrenhiteToCelsius(ftemp);
Console.WriteLine(tempc);
p1.menu(); // if user wants to make some more conversion he can go again //to start and make his choice again
}
else if(choice=="2")
{
Console.WriteLine("Enter the value in Miles");
double miles=Convert.ToDouble(Console.ReadLine());
double km=p1.milestoKilometers(miles);
Console.WriteLine(km);
p1.menu(); // if user wants to make some more conversion he can go again //to start and make his choice again
}
else if(choice=="3")
{
Console.WriteLine("Enter the value in Liters");
double liters=Convert.ToDouble(Console.ReadLine());
double gallon=p1.litersToGallons(liters);
Console.WriteLine(gallon);
p1.menu(); // if user wants to make some more conversion he can go again //to start and make his choice again
}
else if(choice=="4")
{
Console.WriteLine("Exiting !");
return; // exiting from code
}
else {
Console.WriteLine("Please select correct Choice"); // if user make incorrect choice then the program //will exit
}
}
public static void Main()
{
Program p1=new Program();
p1.menu(); // Calling menu function
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.