Advertisement
Guest User

Program

a guest
Feb 11th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void GetChoice(ref int ichoice)
  4.         {
  5.             Console.Write("Enter choice: ");
  6.             string input = Console.ReadLine();
  7.            
  8.             bool result = int.TryParse(input, out ichoice);
  9.  
  10.             if (!result)
  11.             {
  12.                 while (!result && ichoice > 3)
  13.                 {
  14.                     Console.WriteLine("Invalid value.Try again:");
  15.                     input = Console.ReadLine();
  16.                     result = int.TryParse(input, out ichoice);
  17.                 }
  18.             }
  19.         }
  20.  
  21.         static void Main(string[] args)
  22.         {
  23.             double fTemp;
  24.             double cTemp;
  25.             double convertC;
  26.             double convertF;
  27.  
  28.             Console.WriteLine("Welcome to the temperature conversion application");
  29.             Console.WriteLine("_________________________________________________");
  30.             Console.WriteLine("1. Fahrenheit to Celsius");
  31.             Console.WriteLine("2. Celsius to Fahrenheit");
  32.             Console.WriteLine("3. Exit");
  33.            
  34.             int ichoice = 0;
  35.             GetChoice(ref ichoice);
  36.             do
  37.             {
  38.  
  39.                 if (ichoice == 1)
  40.                 {
  41.                     Console.WriteLine("Enter Fahrenheit temperature: ");
  42.                     fTemp = int.Parse(Console.ReadLine());
  43.                     convertC = ConvertCelcius(fTemp);
  44.                     Console.WriteLine(fTemp + "Fahrenheit is " + convertC + "Celsius");
  45.                     Console.WriteLine("Welcome to the temperature conversion application");
  46.                     Console.WriteLine("_________________________________________________");
  47.                     GetChoice(ref ichoice);
  48.                 }
  49.                 if (ichoice == 2)
  50.                 {
  51.                     Console.WriteLine("Enter Celsius temperature: ");
  52.                     cTemp = int.Parse(Console.ReadLine());
  53.                     Console.WriteLine(cTemp + "Celsius is " + cTemp + "Fahrenheit");
  54.                     Console.WriteLine("Welcome to the temperature conversion application");
  55.                     Console.WriteLine("____________________________________________________");
  56.                     GetChoice(ref ichoice);  
  57.  
  58.                 }
  59.                 if (ichoice == 3)
  60.                 {
  61.                     Console.WriteLine("Thank you for using the temperature conversion application. Please come again.");
  62.                 }
  63.                 else
  64.                 {
  65.                     Console.WriteLine("Invalid choice. Please choose again!");
  66.                 }
  67.  
  68.             }
  69.             while (ichoice < 3);
  70.          
  71.         }
  72.         static double ConvertCelcius(double c)
  73.         {
  74.             double f;
  75.  
  76.             return f = 9.0 / 5.0 * c + 32;
  77.         }
  78.         static double ConvertFahrenheit(double f)
  79.         {
  80.             double c;
  81.  
  82.             return c = 5.0 / 9.0 * (f - 32);
  83.  
  84.         }
  85.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement