Visual Basic 2013 Lesson 11: Managing Mathematical Operations in VB2013

[Lesson 10] << [Contents] >> [Lesson 12]

Mathematical operations form an integral part of computer programming. In VB2013, we can write code to instruct the computer to perform mathematical calculations. By writing appropriate VB code, we can create applications that are able to solve complex mathematical problems. For example, we can write VB programme like simultaneous equations Solver and Factors Finder. Besides, we need to use mathematical operations to create applications like the slot machine, star war, amortization calculators and more.

For VB2013 to carry out mathematical operations, we need to write code that uses arithmetic operators. The VB2013 mathematical operators are very similar to the normal arithmetic operators, only with some slight variations. The list of VB 2013 mathematical operators are shown in table 11.1 below:

Table 11.1: Mathematical Operators

Operator Mathematical Function Example
+ Addition  1+2=3
 Subtraction  10-4=6
^  Exponential  3^2=9
*  Multiplication  5*6=30
/  Division  21/7=3
Mod  Modulus(returns the remainder of an integer division)  15 Mod 4=3
\  Integer Division(discards the decimal places)  19/4=4

Example 11.1

In this example, we insert two text boxes, four labels and a button. Click the button and enter the code as shown below. When you run the program, it performs four basic arithmetic operations and displays the results on the four labels.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim num1, num2, difference, product, quotient As Single
num1 = TextBox1.Text
num2 = TextBox2.Text
product = num1 * num2
Label3.Text = product
Label4.Text = quotientEnd Sub

Example 11.2

This program employs the Pythagoras Theorem to calculate the length of hypotenuse c given the length of the adjacent side a and the opposite side b. For those of  you who have forgotten the formula for the Pythagoras Theorem, it is written as

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim a, b, c As Single
a = TextBox1.Text
b = TextBox2.Text

End Sub

Example 11.3: BMI Calculator

A lot of people are obese now and it could affect their health seriously . Obesity has proven by the medical experts to be a one of the main factors that brings many adverse medical problems, including the the cardiovascular disease. If your BMI is more than 30, you are considered obese. You can refer to the following range of BMI values for your weight status.

Underweight = <18.5
Normal weight = 18.5-24.9
Overweight = 25-29.9
Obesity = BMI of 30 or greater

To calculate your BMI, you don’t have to consult your doctor, you can simply use a calculator or a DIY computer program, this is exactly what we are showing you here. The BMI calculator is a Visual Basic program that can calculate the body mass index, or BMI of a person based on his or her body weight in kilogram and the body height in meter. BMI can be calculated using the formula weight/( height )^2, where weight is measured in kg and height in meter. If you only know your weight and height in lb and feet, then you need to convert them to the metric system .

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArsgs) Handles Button1.Click

Dim height, weight, bmi As Single
height = TextBox1.Text
weight = TextBox2.Text
bmi = (weight) / (height ^ 2)
Label4.Text = bmi

End Sub

The output is shown in the Figure 11.1 below. In this example, your height is 1.80m( about 5 foot 11),your weight is 75 kg( about 168Ib), and your BMI is about 23.14815. The reading suggests that you are healthy. (Note; 1 foot=0.3048, 1 lb=.45359237 kilogram)


From the above examples, you can see that writing visual basic 2013 code that involve arithmetic operations is relatively easy. Here are more arithmetic projects you work on:

Area of a triangle
Area of a rectangle
Area of a circle
Volume of a cylinder
Volume of a cone
Volume of a sphere
Compound interest
Future value
Sum of angles in polygons
Conversion of lb to kg
Conversion of Fahrenheit to Celsius

[Lesson 10] << [Contents] >> [Lesson 12]

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on Tumblr