# Arithmetic calculations in

visual basic

• WRITING SIMPLE ARITHMETIC PROGRAMS
IN ANY PROGRAMMING LANGUAGE IS
IMPORTANT FOR INTRODUCING
PROGRAMMING CONCEPTS

• CONSIDER THE FOLLOWING ARITHMETIC
EXAMPLES.
EXPRESSION SOLVER
THE PROGRAM
• Private Sub Exit_Click()
End
End Sub
Dim a, b, c, det As Integer
Dim root1, root2 As Single
End Sub
• Private Sub new_Click()
' To set all values to zero
Coeff_a.Text = ""
Coeff_b.Text = ""
Coeff_c.Text = ""
txt_root1.Visible = False
txt_root2.Visible = False
txt_root1.Text = ""
txt_root2.Text = ""
Lbl_and.Visible = False
Lbl_numroot.text = ""
End Sub
• Private Sub Solve_Click()
• a = Val(Coeff_a.Text)
b = Val(Coeff_b.Text)
c = Val(Coeff_c.Text)
• 'To compute the value of the
determinant
det = (b ^ 2) - (4 * a * c)
If det > 0 Then
lblroots.text = 2
root1 = (-b + math.Sqrt(det)) / (2 * a)
root2 = (-b – math.Sqrt(det)) / (2 * a)
lblAnswers.text = "The roots are "
LblAnd.Visible = True
txt_root1.Visible = True
txt_root2.Visible = True
txt_root1.Text = root1
txt_root2.Text = root2
• ElseIf det = 0 Then
root1 = (-b) / 2 * a
Lbl_numroot.TEXT = 1
lblAnswers.TEXT = "The root is "
txt_root1.Visible = True
txt_root1.Text = root1
Else
Lbl_numroot.text = 0
lblAnswers.text = "There is no root "
End If
End Sub

Sample output
Alternatively
• Dim a, b, c, undroot, root, posx, negx, x1, x2
As Single

Private Sub cmdcompute_Click()
a = txta.Text
b = txtb.Text
c = txtc.Text
If txta.Text = "" Then
MsgBox ("Please enter the value of A ", 0 +
32)
Exit Sub
End If
• If txtb.Text = "" Then
MsgBox ("Please enter the value of B ", 0
+ 32 )
Exit Sub
End If
If txtc.Text = "" Then
MsgBox ("Please enter the value of C ", 0
+ 32 )
Exit Sub
End If
• If a = 0 Then
MsgBox "A can't equal 0 ", 0 + 32
Exit Sub
End If
If (b ^ 2) - 4 * a * c < 0 Then
txtx1.Text = ""
txtx2.Text = ""
MsgBox "Imaginary solution : (b^2)-4*a*c < 0",
0 + 48
Exit Sub
End If
• 'Calcualtions
undroot = (b ^ 2) - 4 * a * c
root = undroot ^ (0.5)
posx = (b * -1) + root
negx = (b * -1) - root
x1 = posx / (2 * a)
x2 = negx / (2 * a)
'Displaying results
txtx1.Text = " " & x1
txtx2.Text = " " & x2
End Sub
EXAMPLE 2: Maximum Number calculator
• This program let users input three
hidden numbers and the program can
calculate the maximum number. In
order to set the textbox in password
mode, you have to set the
symbols such as * . The program
created a function calMax with three
parameters x,y, z and then uses a
procedure to call this function. It uses
a simple If Then ElseIf statements to
determine the maximum number. The
function Str is used to convert a
numeric to string.
• Function calMax(x, y, z As Variant)
If x > y And x > z Then
calMax = Str(x)
ElseIf y > x And y > z Then
calMax = Str(y)
ElseIf z > x And z > y Then
calMax = Str(z)
End If
End Function
• Private Sub Command1_Click()
Dim a, b, c
a = Val(Txt_Num1.Text)
b = Val(Txt_Num2.Text)
c = Val(Txt_Num3.Text)
Lbl_Display.text= calMax(a, b, c)
End Sub

Private Sub Label5_Click()
End Sub
End Sub
Example 3: PYTHAGORUS THEOREM PROGRAM
THE CODE
• Private Sub Command1_Click()
Dim AB, AC, BC As Single
AB = Val(Txt_AB.Text)
AC = Val(Txt_AC.Text)
BC = Val(Txt_BC.Text)
If AB <> 0 And AC <> 0 Then
BC = Math.sqr(AB ^ 2 + AC ^ 2)
• End Sub
Example 4: Simultaneous linear equation solver
code
• Private Sub Command1_Click()
Dim a, b, c, d, m, n As Integer
Dim x, y As Double
a = Val(Txt_a.Text)
b = Val(Txt_b.Text)
m = Val(Txt_m.Text)
c = Val(Txt_c.Text)
d = Val(Txt_d.Text)
n = Val(Txt_n.Text)
x = (b * n - d * m) / (b * c - a * d)
y = (a * n - c * m) / (a * d - b * c)
Lbl_x.text = Round(x, 2)
Lbl_y.text = Round(y, 2)
• End Sub
• Private Sub Command2_Click()
Txt_a.Text = ""
Txt_b.Text = ""
Txt_m.Text = ""
Txt_c.Text = ""
Txt_d.Text = ""
Txt_n.Text = ""
Lbl_x.Caption = ""
Lbl_y.Caption = ""
End Sub
• Explanation:
• -Linear simultaneous equations take the following
forms:
• ax+by=m
• cx+dy=n
• -Simultaneous equations can normally be solved
by the substitution or elimination methods. In this
program, I employed the substitution method. So, I
obtained the following formulae:
• x = (b * n - d * m) / (b * c - a * d)
y = (a * n - c * m) / (a * d - b * c)
• -To limit the answers to two decimal places, I used
the round function.
•
REVIEW EXERCISEs – TRY ON YOUR
OWN
1.Write a VB program to calculate the area of a
circle given PI= 3.14 and that
2. Write a VB program to calculate the VOLUME of a
cube given that
VOLUME= LENGTH*WIDTH*HEIGHT
3. Write a VB program to calculate the area of a
triangle given that
AREA= ½ *BASE*HEIGHT
4. Write a VB program that enables the user to
choose the Item purchased from a list a list box, or a
combo box then inputs the price and quantity
purchased. The program then computes the
discount and the total amount to be paid. Assume a
discount of
5%
Write VB program to calculate the
following:
• 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
• Mean
• Variance
• Sum of angles in polygons
• Converst lb to kg
• Convert Fahrenheit to Celsius
• .5. The income earned by a salesman is calculated as shown
below.
• Fixed allowance = 5000
• Commission (based on sales)
• Sales range(kshs)Commission(%)
• From 10,000 but less than 20,000 7
• From 20,000 but less than 50,000 9
• From 50,000 but less than 70,000 12
• From 70,000 and above 15
• Gross Income= Fixed allowance + Commission
• Tax= 10 % of Gross Income of at least 10,000
• Net Income = Gross Income-Tax
(a) Write a VB program to input the name and sales amount of
a given salesman then calculates the Net Income.
(•)(b) Modify your program to input the names and sales of 10
salesmen and compute the Net income of each.
MID SEMESTERS PROJECT
2013)
WORK
• PROJECT 1 (10 marks) DUE ON 11 / 2/

• (a) Write a VB program to input the Basic salary of an employee, his
Grade AND MODE OF TRANSPORT( I.E WHETHER BY PERSONAL CAR OR BY
BUS). The program computes the Gross salary and Net pay as follows.
• Allowances::
• (i) House Allowance = 35 % of Basic salary
• (II) Transport allowance (calculated based on the following)
• -10,000 for those who use personal par
• -For those who use bus
• 2000 for grades 1- 2
• 4,000 for grades 6 and above
• Gross salary = Basic salary + Allowances
• Tax is computed based on the table below

• Basic Salary Range Tax Rate ( % of Basic Pay)

• Less than 10,000 No tax
• At least 10,000 but less than 30, 000 10
• At least 30,000 but less than 100, 000 13
• At least 100,000 16

• NET PAY = Basic pay + allowances - Tax
• (b) Modify your code in the above program to process the
salaries of 10 employees.
END OF SEMESTERS PROJECT-10
marks (Due on the Wednesday of the last week of learning)
• Develop a project in an area of your interest using Visual Basic
programming language that demonstrates your mastery of all the
programming skills learned in class and self taught. Some of the
possible areas you may want to consider for a project may include
but are not limited to: