Jump to content

A powers calculator - C#

0
  Ángel Manuel García Carmona's Photo
Posted Dec 14 2010 02:17 PM

In this article, i will explain how to create a powers calculator (in C#), using the Math.Pow method to calculate them.

First, open Visual Studio (or C# Express Edition) and go to "File>New project>Visual C#" and we select Windows Forms Application.

After this step, insert two textbox, one button and two labels in the form, and after that, change the text of them.

Once that you have inserted the elements, create a method named clcpowerexp2(), writing the following code:
private void clcpowerexp2()
        {
            int numeroaelevar = int.Parse(textBox1.Text); // base
            int expnt = 2; // exponent
            double outcome = Math.Pow(numeroaelevar, expnt);
            textBox1.Text = outcome.ToString(); // result
        }

Like you can see in the code, you must assign the numbers at int array because if not, the code will have errors.
Posted Image
Now, write in the event Click of the button the name of the method, in this case clcpowerexp2() and some lines, like this:
private void button1_Click(object sender, EventArgs e)
        {
            clcpowerexp2();
            label1.Text = "Value";
            label2.Hide();
            textBox2.Hide();
        }

Once that you have done the last steps, press Ctrl+F5 to run the application. If you go to the bin subfolder of your project, you can see and execute the executable of this application.
Posted Image
You can download this example: Attached File  powers calculator.zip (41.92K)
Number of downloads: 297

Tags:
0 Subscribe


0 Replies