In the part article that I have already wrote this type of
program about factorial of numbers if you are wondering why I wrote this
another factorial program my answer is very simple to give you an idea how to
write a factorial program using Microsoft Visual Basic 6 as our programming
language for program development. As a programmer we are not always dealing
with one programming language all the time the reason is the programming has a
wide range of application and not all programming language fits in a particular
application. I will give you some insights if you want to create a program that
deals with the hardware of the computer you can use C++ or C to handle for a
job because it is designed for systems programming you can’t use this
programming language in making webpage because it is not designed with it use
HTML and CSS instead. I used Microsoft
Visual Basic 6 in this application because of its ease of use I my thesis in my
college days we used it as our programming language for our thesis in titled “Computers
Semi-Encyclopaedia” what is good about this language is that you can draw the
different objects or I would say control and then after you design you can put
the code for its control and run the program. It can save you big amount of
time in designing you application you can more focus in solving the program is
a short period of time.
This number factorial program is based on the concept in
computer science called recursion algorithm. I will give you some idea what is
a recursion is all about. In simple words we can say recursion is the ability
of the function to call itself several times during the program execution in
order to solve the program one of the basic application of recursion is
factorial of numbers, tower of Hanoi and Fibonacci number as an example. The recursion algorithm was developed by Dr.
Stephen Cole Kleene in 1930 he is an American mathematician and a computer
scientist who contributed many concepts in computer science.
Before we can write a single line of code for our program number
factorial program we need to design our user interface. In our form we put a
label that tells the user to enter a number take note we are dealing here with whole
positive number and then it followed by our textbox where the use put a
value. The next portion is we have two
command button the first button will performed computation and the other one
will allow the user to quit the program. In order for us to display the list of
numbers I am using List Box control in Visual Basic that advantage of using
this control is that is automatically put a vertical scroll bar when the values
exceed the size of our list box as we put in our window form.
Here is the function that I wrote for solving the factorial
value of a number. What this function will do is to accept a number as its
parameter in this case variable y if y <= 1 then the value is 1 it means if
the user will only give 1 as the number in our text box it will return 1 as its
default factorial value. If the value of
variable y is greater than 1 it will call itself again and decrement the value of y until such
time we arrive with the final answer let say the user will give 4 as its
factorial value 4! = 4! * 3! * 2! * 1! = 24.
So the final value of 4! Is 24 we subtract 1 every time our loop
statement call itself. The important of
using function compared with writing the code in a straight forward manner is
that we can call the function several times in our program without writing the
same code all over again it helps us to cut down the development time of our
program and minimize the errors that might occur in during program development
process.
Private Function
Solve_Factorial(ByVal y As Double) As Double
If y <= 1 Then
Solve_Factorial = 1
Else
Solve_Factorial = y * Solve_Factorial(y -
1)
End If
End Function
In the general declaration we use Option Explicit it means
every variable in our program should be declared with its corresponding data
types. It is very important to declare
all the variable with its corresponding data type because we will know what is
corresponding value should be used in our program. The next portion our code is
the call of solve button here we have two variable a and b we declare it
integer as its data types. We clear the list box values by calling this command
lstValues.Clear the b variable is being used as our assignment operator we
assigned for Val(Form1.txtvalue.txt) the keyword Val is a function In Visual
Basic 6 to convert text value in a text box into an integer format. As we go along we use a for loop statement
variable a = 0 to b will add the items in our list box by using this commands For a = 0 To b lstValues.AddItem Format$(a, "@@")
& "! = " & Solve_Factorial((a)) and calling our
function called Solve_Factorial(a) and we exit in our loop statement.
Private Sub
cmd_solve_Click()
Dim a As Integer, b As Integer
Call lstValues.Clear
b = Val(Form1.txtvalue.Text)
For a = 0 To b
lstValues.AddItem Format$(a, "@@")
& "! = " & _
Solve_Factorial((a))
Next a
End Sub
The last portion of our code is the quit bottom what is code
will do when the user click the bottom it will ask the use if the user want to
quit the program or not is the user will continue to use the program it will
return to the form window and erase the previous value given by the user. If
the use choose to quit the program will be terminated and the user return to
the windows environment.
Private Sub
cmdquit_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
Form1.Show
Form1.txtvalue.SetFocus
End If
End Sub
The program Number Factorial program is not difficult to
write and develop what is very important for us to know the is our desired
output and the formulas need to solve that particular problem in hand. If you
have some questions feel free to email me I am very happy to answer your
questions , comments and suggestions.
Thank you very much and Happy Productive Programming.
If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com.
No comments:
Post a Comment