How to write a Visual Basic(VB) program for Addition, Subtraction, Multiplication, and Division of two numbers with examples.
Addition, Subtraction, Multiplication, and division are the most basic mathematical operation and it can be a best practice to learn any programming language.
Let’s see how can we perform simple mathematical operations in visual basic. Check more Visual Basic programs for beginners.
Visual basic program to Add two numbers
To add two numbers in visual basic is really simple just use (+) operator. the below code will print the sum of two numbers: 30.
Console.WriteLine("Sum of two numbers: " & (10 + 20))
Visual basic program to Subtract two numbers
To subtract two numbers is visual basic. we can use (-) operator. the below code will print Subtraction of two numbers: 10
Console.WriteLine("Subtraction of two numbers: " & (30 - 20))
Visual basic program to Multiply two numbers
Use (*) operator to multiply two numbers in visual basic. The output of the below code will be Multiplication of two numbers: 200.
Console.WriteLine("Multiplication of two numbers: " & (10 * 20))
Visual basic program to Divide two numbers
Use (/) operator to divide two numbers in visual basic. The output of the below code will be Division of two numbers: 2.
Console.WriteLine("Division of two numbers: " & (20 / 10))
Write a visual basic program to perform basic mathematical operations.
Below is the code to perform basic mathematical operations like Addition, Subtraction, Multiplication, and division in visual basic. In this example, we have two numbers a = 10 and b =5 and we are going to perform all mathematical operations on the same numbers.
Module Module1 Sub Main() Dim a As Integer Dim b As Integer a = 10 b = 5 'Sum of a And be Console.WriteLine("Sum of two numbers: " & (a + b)) 'Submission of two number Console.WriteLine("Submission of two numbers: " & (a - b)) 'Division of two number Console.WriteLine("Div of two numbers: " & (a / b)) 'Multiplication of two number Console.WriteLine("Mul of two numbers: " & (a * b)) Console.ReadLine() End Sub End Module
How to create a calculator in visual basic.
Below is the code to create a calculator in visual basic. This is the GUI program where the user will enter the number in the input text field and the output generates accordingly. Create a calculator application in visual basic.
Private Sub cmdClear_Click() Text1.Text = “” Text2.Text = “” Text3.Text = “” Text1.SetFocus End Sub Private Sub cmdExit_Click() Unload Me End Sub Private Sub mnuadd_Click() Text3.Text = Val(Text1.Text) + Val(Text2.Text) End Sub Private Sub mnuDiv_Click() Text3.Text = Val(Text1.Text) / Val(Text2.Text) End Sub Private Sub mnuMult_Click() Text3.Text = Val(Text1.Text) * Val(Text2.Text) End Sub Private Sub mnusub_Click() Text3.Text = Val(Text1.Text) – Val(Text2.Text) End Sub
Calculator application in visual basic using forms
Write a visual basic program to create a calculator application using forms. Create a simple application in visual basic.
Public Class Form1 Dim a As Integer Dim b As Integer Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Sum.Click a = TextBox1.Text b = TextBox2.Text Label2.Text = (a + b) End Sub Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click a = TextBox1.Text b = TextBox2.Text Label2.Text = (a - b) End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click a = TextBox1.Text b = TextBox2.Text Label2.Text = (a * b) End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click a = TextBox1.Text b = TextBox2.Text Label2.Text = (a / b) End Sub End Class
Output: