Showing posts with label visual basic power of a number. Show all posts
Showing posts with label visual basic power of a number. Show all posts

Saturday, April 4, 2015

Power of a Number in Visual Basic 6


One of the interesting aspects of computers it is designed to perform computation as its first real application to mankind.  Very fast computation that even a human being that is very intelligent in the field of mathematics cannot compete with the speed and accuracy of the computer provided that the program that is being stored and run is correct and there is not errors or bugs on it.  For the information of our readers of this article the first electronic computer was ABC computer that was invented and developed by Dr. John D. Atanasoff and his student Clifford Berry. When we say it first electronic computer it means it doesn’t use any mechanical part not any moving parts all the operation that is being perform is done electronically by switches and logic gates.  This astonishing ability of the computer prove to be useful to us we when visit a bank we can check how much money that we have deposit or withdraw without going to the manual process of opening the filing cabinet and checking those enormous files of papers it save us big amount of time and we can lessen the errors on the part of the teller in the bank for example.

In today’s article I will discuss a very simple program called Power of a Number using Microsoft Visual Basic 6 as our programming language.  If you are new with programming you may ask the question what the heck is Visual Basic is all about why it is so different with other programming language that is being used in school, industry and company. Well before I answer that question let me give you a brief history on how Microsoft Visual Basic 6 came to life. The BASIC (Beginners All-Purpose Symbolic Instruction Code) is a programming language developed in Darthmouth College in Delaware, USA at 1963 by Dr. John Kemeny and Thomas Kurtz their intention is to created a programming language that is easier to learned and understand by students and those people that are not technically inclined with the use computers during those years computers are mainly used by scientist, engineers and technician alike. They want to make programming accessible to common man that’s why they write the first compiler of BASIC.  As the language improved dramatically over the years of use Mr. Allan Cooper the father of Visual Basic would like to create a programming language that handles the design of the graphical user interface of the user by not writing down a single line of code for the design but rather putting a series of controls that a programmer can use to draw the different objects in the form where the program is being developed. The year 1991 Microsoft Corporation headed by Bills Gates collaborate with Allan Cooper to launch a new type of programming language based on BASIC they called it Visual Basic which become of the most highly successful programming language of the world.
One of the most basic feature of Microsoft Visual Basic that cannot be found on other programming language during its introduction in 1991 is that the ability of the programming to draw first the user interface without using a code for the layout and design and after that the use of minimal amount of code to achieve its desired program result. The obvious benefit of using this programming paradigm it cuts down the big amount of time of system development and the programmer can focus more on solving the real problem in programming.  I hope you have learned something on the brief history of Visual Basic now let us go down of our problem Power of a Number.
Basically Power of a Number program it will performed a series of computations to derive the result when the user will give the base and the exponent value of the number.  The base number is the number that we want to raise its exponent value let say 5 is our base value when we want to raise it to the exponent 3 the result will be 5 * 5 * 5 is equal to 125 we just multiply our base number with itself by 3 times of the exponent value that we have given is 3 very simple indeed.

Before we can write a single line of code of our program we would like to design its graphical user interface this user interface is the one that will interact to our end user of our program. We have here three labels the first label will ask the user to enter the base value of the number, the second label will ask the user to enter the exponent value that we like and the third label is the lbl_result.caption it is designed to display the result once the use click the compute button of our program. Below here it the code in our compute button that will perform a computation to find the power value of the number.

The first line of command is option explicit this command will force the programmer to declare all the variables in our program. In Microsoft Visual Basic the language can allow the programmer to use a variable without declaring its data types in our case I use this command so that every variable that I use I will be force to declare it before I used it. It is a good programming practices that we must to follow always in our programming projects or assignments .

We three variables base, exponent, value and result we declare this four variables as integer it means we are dealing with whole number, positive, negative numbers including zero. The exponent variable here will act as our assignment system that will hold the value in our control Val(txt_exponent.text) and base with a value base = Val(txt_base.txt) take note readers the text field in Visual Basic 6 by nature the value that is being handled is string we are using Val() function to convert string value into integer format.

The next portion of our code we assign result with a value of 1 this mean this is the starting point of our initial value of 1  the purpose of result variable in our program is that it will accumulate the value of base with respect to the value of our exponent variable.  We are using a for loop statement below to repeat the process of multiplication of our base and result variable. After the series of repetition of our looping statement we display the result using a label control in this case we are using this command lbl_result.Caption = base & " to the power " & exponent &   " is " & result & "." this command tells us to display the value of base the ampersand symbol separate the string “to the power “ and exponent and it the result as we call our assignment statement to display the computed value of variable result.

Option Explicit

Private Sub cmd_compute_Click()
Dim base, exponent, value, result As Integer
exponent = Val(txt_exponent.Text)
base = Val(txt_base.Text)
result = 1
For value = 1 To exponent
   result = result * base
Next value
lbl_result.Caption = base & " to the power " & exponent &    " is " & result & "."
End Sub

This command button quick will display a dialog box and will ask the end user whether he or she would like to quit using our program Power of a Number if the user reply yes it will terminate the program using the command unload me if no it will display the form once again and clear all the values in the textbox including our label control.

Private Sub cmd_quit_Click()
Dim reply As String
reply = MsgBox("Do you want to quit program?", vbYesNo + vbQuestion + vbDefaultButton2, "Quit Program")
If (reply = vbYes) Then
  Unload Me
  Else
  frm_power.Show
  frm_power.lbl_result.Caption = ""
  frm_power.txt_base.Text = ""
  frm_power.txt_exponent.Text = " "
  frm_power.txt_base.SetFocus
 End If
End Sub

Writing this program is not difficult what is important you know the purpose and the formula we need in our program. A good programming tip that I can give to you plan first before you write a single line of code it will save you big amount of time and you can more focus in solving the problem on hand more effectively and efficiently. If you have some questions, clarifications or comments please email me I am very glad to answer those queries or questions you have in mind.

If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com

People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360


Thank you very much and Happy Programming.




Sample Program Output