# Getting Started With Arithmetic Operators In F#

Introduction

In this article I have demonstrated how to perform arithmetic operations, such as addition, subtraction, multiplication, division and modulus in Windows Forms applications.

Arithmetic Operators

• Multiplication (*)
• Subtraction (-)
• Division (/)
• Modulo (%)

Now let's use the following procedure.

Step 1:

Open Visual Studio then select "Create New Project" --> "F# Console Application".

Step 2:

Now go the Solution Explorer on the right side of Visual Studio. Right-click on "References" and select "Add references".

Step 3:

After selecting "Add References", in the framework template you need to select "System.Windows.Forms,System.Drawing" while holding down the Ctrl key and Click on "Ok."

Step 4:

Write the following code in the F# applcation:

//uses the F# standard library

open System

//specifies the location of the Drawing classes

open System.Drawing

//specifies the namespace memory location of the form class

open System.Windows.Forms

//creates a new form

let addform=new Form(Text="Airthmatic Operations", Size=new System.Drawing.Size(800, 700),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)

//creates a label

let n1label=new Label(Text="First Number:",Top=20,Left=10,AutoSize=true

let firsttextbox=new TextBox(Location=new System.Drawing.Point(100, 20))

//creates another label and change its text to "Second number:"

let n2label=new Label(Text="Second Number:", Top=20,Left=5,Location=new System.Drawing.Point(0,50),AutoSize=true

let secondtextbox=new TextBox(Location=new System.Drawing.Point(100,50))

//creates another label and change its text to sum

let n3label=new Label(Text="Sum:", Location=new System.Drawing.Point(0, 90),AutoSize=true

let n4label=new Label(Text="Multiply:",Location=new System.Drawing.Point(0, 120),AutoSize=true

let n5label=new Label(Text="Subtraction:",Location=new System.Drawing.Point(0, 150),AutoSize=true

let n6label=new Label(Text="Division:",Location=new System.Drawing.Point(0, 180),AutoSize=true

let n7label=new Label(Text="Modulous:",Location=new System.Drawing.Point(0, 210),AutoSize=true

//creates a label that will display the result of the computation

let ansmullabel=new Label(Location=new System.Drawing.Point(80, 120))

let anssublabel=new Label(Location=new System.Drawing.Point(80, 150))

let ansdivlabel=new Label(Location=new System.Drawing.Point(80, 180))

let ansmodlabel=new Label(Location=new System.Drawing.Point(80, 210))

//make buttons

let mulbutton=new Button(Text="Multiply",Location=new System.Drawing.Point(200,230))

let subbutton=new Button(Text="Subtraction",Location=new System.Drawing.Point(300,230))

let divbutton=new Button(Text="Division",Location=new System.Drawing.Point(400,230))

let modbutton=new Button(Text="Modulous", Location=new System.Drawing.Point(500, 230))

let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(600, 230))

//add the controls into the form

//when the add button is clicked

//convert the textbox values to unsigned int

let firstnum=Convert.ToUInt32(firsttextbox.Text)

let secondnum=Convert.ToUInt32(secondtextbox.Text)

let sumval=firstnum + secondnum

//when the Multiply button is clicked

//convert the textbox values to unsigned int

let firstnum=Convert.ToUInt32(firsttextbox.Text)

let secondnum=Convert.ToUInt32(secondtextbox.Text)

let mulval=firstnum * secondnum

//display the multiplication value in the ansmullabel

ansmullabel.Text<-Convert.ToString(mulval))

//when the Subtraction button is clicked

//convert the textbox values to unsigned int

let firstnum=Convert.ToUInt32(firsttextbox.Text)

let secondnum=Convert.ToUInt32(secondtextbox.Text)

let subval=firstnum - secondnum

//display the Subtract value in the anssubvlabel

anssublabel.Text<-Convert.ToString(subval))

//when the Division button is clicked

let firstnum=Convert.ToInt32(firsttextbox.Text)

let secondnum=Convert.ToInt32(secondtextbox.Text)

let divval=firstnum / secondnum

//display the Division value in the ansmullabel

ansdivlabel.Text<-Convert.ToString(divval))

//when the Modulus button is clicked

//convert the textbox values to unsigned int

let firstnum=Convert.ToUInt32(firsttextbox.Text)

let secondnum=Convert.ToUInt32(secondtextbox.Text)

let modval=firstnum % secondnum

ansmodlabel.Text<-Convert.ToString(modval))

//when the exit button is clicked, close the form

Step 5:

Debug the application by pressing F5 and the result will be shown in the application as in the figure given below.

Step 6:

Now you need to enter the first number and second number. With these two numbers we will perform the various arithmetic operations.

Step 7:

Now click on the "Add" Button.

Step 8:

Now click on the "Multiply" button.

Step 9:

Now click on the "Subtraction" button.

Step 10:

Now click on the "Division" button.

Step 10:

Now click on the "Modulus" button.

Summary

In this article you have seen how to perform arithmetic operations, such as addition, subtraction, multiplication, division and modulus. I hope it helps you to understand.

Up Next