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

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

Now let's use the following procedure.

Step 1:

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

create-app.jpg

Step 2:

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

select-references.jpg

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." 

import-namespaces.jpg

Step 4:

Write the following code in the F# applcation:

// Learn more about F# at http://fsharp.net 

//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 ansaddlabel=new Label(Location=new System.Drawing.Point(80, 90)) 

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 addbutton=new Button(Text="Add", Location=new System.Drawing.Point(100, 230)) 

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 

addform.Controls.Add(n1label) 

addform.Controls.Add(firsttextbox) 

addform.Controls.Add(n2label) 

addform.Controls.Add(secondtextbox) 

addform.Controls.Add(n3label) 

addform.Controls.Add(ansaddlabel) 

addform.Controls.Add(n4label)

addform.Controls.Add(ansmullabel)

addform.Controls.Add(n5label)

addform.Controls.Add(anssublabel)

addform.Controls.Add(n6label)

addform.Controls.Add(ansdivlabel)

addform.Controls.Add(n7label)

addform.Controls.Add(ansmodlabel)

addform.Controls.Add(addbutton) 

addform.Controls.Add(mulbutton)

addform.Controls.Add(subbutton)

addform.Controls.Add(divbutton)

addform.Controls.Add(modbutton)

addform.Controls.Add(exitbutton) 

 

//when the add button is clicked 

addbutton.Click.Add(fun addfunction -> 

//convert the textbox values to unsigned int 

let firstnum=Convert.ToUInt32(firsttextbox.Text) 

let secondnum=Convert.ToUInt32(secondtextbox.Text) 

let sumval=firstnum + secondnum 

ansaddlabel.Text<-Convert.ToString(sumval)) 

 

//when the Multiply button is clicked 

mulbutton.Click.Add(fun mulfunction -> 

//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 

subbutton.Click.Add(fun mulfunction -> 

//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 

divbutton.Click.Add(fun mulfunction -> 

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 

modbutton.Click.Add(fun addfunction -> 

//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             

exitbutton.Click.Add(fun exit -> addform.Close())   

addform.Show() 

Application.Run(addform) 

 

Step 5:

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

after-debug.jpg

Step 6:

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

enter-values.jpg

Step 7:

Now click on the "Add" Button.

add.jpg

Step 8:

Now click on the "Multiply" button.

multipyl.jpg

Step 9:

Now click on the "Subtraction" button.

sub.jpg

Step 10:

Now click on the "Division" button.

div.jpg

Step 10:

Now click on the "Modulus" button.

mod.jpg

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
    Ebook Download
    View all
    Learn
    View all