Thread: Farenheight to Celcius ch 3 View Single Post
August 4th, 2009, 09:01 PM
 Lloydering2 Registered User Join Date: Aug 2009 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts
Farenheight to Celcius ch 3

Hi,

I just got my program to work, although I don't understand why it didn't work the first time. Using the formula Celcius = 5/9 * (Farenheight -32) kept giving me a zero in the celcius box no matter what number I put in Farenheight. I got frustrated and gave up. I came back to it today and the idea came to switch the formula around to : Celcius = (Farenheight - 32) * 5 / 9; and it voila it worked. My question is why did the first formula produce zero's? and simply switching the formula around seems to work.
Isn't: Celcius = 5/9 * (Farenheight -32); &
Celcius = (Farenheight - 32) * 5 / 9; The same thing? I got it to work. I supose I probably should use float for the Celcius but I dont think it matters much. Anyhow thanks for your time on this matter.

using System;
using System.Windows.Forms;

public class frmMain : Form
{
private Label label1;
private Label label2;
private Button btnCalculate;
private Label lblCelcius;
private TextBox txtCelcius;
private TextBox txtDegreesF;
#region Windows code
private void InitializeComponent()
{
this.txtDegreesF = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.btnCalculate = new System.Windows.Forms.Button();
this.lblCelcius = new System.Windows.Forms.Label();
this.txtCelcius = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtDegreesF
//
this.txtDegreesF.Location = new System.Drawing.Point(171, 47);
this.txtDegreesF.Name = "txtDegreesF";
this.txtDegreesF.Size = new System.Drawing.Size(100, 20);
this.txtDegreesF.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 50);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(144, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Insert number in Farenheight:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(58, 88);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(98, 13);
this.label2.TabIndex = 3;
this.label2.Text = "Degrees in Celcius:";
//
// btnCalculate
//
this.btnCalculate.Location = new System.Drawing.Point(184, 134);
this.btnCalculate.Name = "btnCalculate";
this.btnCalculate.Size = new System.Drawing.Size(75, 23);
this.btnCalculate.TabIndex = 4;
this.btnCalculate.Text = "Calculate";
this.btnCalculate.UseVisualStyleBackColor = true;
this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
//
// lblCelcius
//
this.lblCelcius.AutoSize = true;
this.lblCelcius.Location = new System.Drawing.Point(171, 88);
this.lblCelcius.Name = "lblCelcius";
this.lblCelcius.Size = new System.Drawing.Size(0, 13);
this.lblCelcius.TabIndex = 5;
//
// txtCelcius
//
this.txtCelcius.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtCelcius.Location = new System.Drawing.Point(171, 81);
this.txtCelcius.Name = "txtCelcius";
this.txtCelcius.Size = new System.Drawing.Size(100, 26);
this.txtCelcius.TabIndex = 6;
this.txtCelcius.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(403, 190);
this.MaximizeBox = false;
this.Name = "frmMain";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

public frmMain()
{
InitializeComponent();
}

public static void Main()
{

frmMain main = new frmMain();
Application.Run(main);
}

private void btnCalculate_Click(object sender, EventArgs e)
{
int Farenheight;
int Celcius;
bool flag;

// input step
// check farenhieght for int

flag = int.TryParse(txtDegreesF.Text, out Farenheight);
if (flag == false)
{
MessageBox.Show("Enter a number", "Input error");
txtDegreesF.Focus();
return;
}

// compute step

Celcius = (Farenheight - 32) * 5 / 9;

// display step

txtCelcius.Text = Celcius.ToString();

}

}