C# Methods using temperature conversion fahrenheit and celcius

/*This is a console application to introduction to methods.

Every program requires a heading, which is below. The fields

will vary according to the class or organization you are

creating this program. For this class we will use temperature

conversions.

 

Program: Temperature conversion method

Programmer: Mike Kniaziewicz

Date: February 26, 2012

Version: 1.0.0 */

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Temperature_conversion_method

{

class Program

{

staticvoid Main(string[] args)

{

/*This is where you declare variables to be used.
I generally place the first letter of the variable type before the variable for readability in the Main function. Here are the formulas we will be using:
C = (F – 32) * 5/9
F = C * 9/5 + 32*/

string scelcius;

//This is a method call to convert the value entered to fahrenheit

convert_to_fahrenheit();

//This is a method call to convert the value entered to celcius

Console.WriteLine(“Enter a temperature to convert to Fahrenheit:”);

scelcius = Console.ReadLine();

Console.WriteLine(“Celcius Conversion Method: You entered {0} C, fahrenheit is {1:F0}\n”, scelcius, convert_to_celcius(scelcius));

//Console.ReadKey will keep the Console application open waiting for a keyboard response

Console.ReadKey();

}

//A public method to perform the fahrenheit calculations

publicstaticvoid convert_to_fahrenheit()

{

double dcelcius, dfahrenheit;

Console.WriteLine(“Enter a temperature to convert to celcius:”);

//always returns a string value,so you will need to parse (convert) it to a double

dfahrenheit = double.Parse(Console.ReadLine());

/* For math always remember Please (Parenthesis) Excuse(Exponents)

My (Multiplication) Dear (Division) Aunt (Addition)  Sallie (Subtraction)*/

dcelcius = (dfahrenheit – 32) * 5 / 9;

//Format the double output, so there are 0 decimal places

Console.WriteLine(“You entered {0:F0} F, celcius is {1:F0}\n”, dfahrenheit, dcelcius);

}

publicstaticdouble convert_to_celcius(string scelcius)

{

double dfahrenheit;

//Convert Celcius to Fahrenheit; however do not convert the string

//until performing the math           

dfahrenheit = double.Parse(scelcius) * 9 / 5 + 32;

return dfahrenheit;

}

}

}

Comments are closed.