a lot of my friends and kids i tutor have issues with finding the highest common factor (HCF) of any bunch of numbers. This is mostly because in my country education is really sub-standard. kids usually resort to rote learning and hard rules.
In this sense i created a program with which calculates HCF.
although this cannot actually be done by hand and there are even easier and simpler ways to get HCF i personally think that this is the most primitive, and hence the most basic of techniques. i hope people will be able to understand the nature of HCF.
the programming language i'm going to write in today is Microsoft studio 2010 in console mode
this is because it isn't so case sensitive and it is very user friendly thus perfect for an aspiring beginner.
Step 1: Step 1:declaring the Variables
in any program when we want to store any form of data for manipulation we need to declare variables.Although there are many types in my program i've only used local variables.
it stores integral variables in the format
<p>Dim x As Integer</p>
this labels variable by the name "x" as an integeral data type
so in the program we have to declare these variables
Dim firstNum, secondNum, TEMP, HCF As Integer
i've basically stored for variables by the names of : firstNum,secondNum, TEMP, HCF
Step 2: Storing the Variables
Once we've declared a variable we have to assign it a value otherwise it's useless.
to do this we make use of the "=" operator
but to have it read from the user we need a way for it to be entered. we use the "Console.ReadLine" function
this is a function of visual basic's console mode which reads a line typed into the console
the program goes like this;
firstNum = Console.ReadLine
we then do the same with the next variable
secondNum = Console.ReadLine
this stores two numbers for manipulation by the program
Step 3: Comparing
next we compare two variables and check which one is smaller. we could use the larger number too but it would be useless to put excess load on the program. but if both variables are equal we can use either
to compare we use the if statements
<p>If <em>condition</em> Then<br><em>(action if condition is true)</em></p><p>ElseIf <em>condition</em> then</p><p><em>(action if condition is true)</em></p><p>End If</p>
so in effect it looks like this
<p>If firstNum < secondNum Then<br> TEMP = firstNum ElseIf firstNum > secondNum Then TEMP = secondNum</p><p> ElseIf firstNum = secondNum Then</p><p> TEMP = secondNum End If</p>
Step 4: Finding HCF
in theory HCF is the highest integer by which all the given numbers can be individually divided by without leaving a remainder. or in a computer's senses a remainder of zero
in my program i keep dividing the numbers and increasing until i get the highest integer possible which divides all the numbers without leaving a remainder.
for this i will use a "for iteration loop"
the syntax goes:
<p>For i = <em>(any number)</em> to <i>(any number)</i> step <i>(incremental number)</i></p><p><em>(function)</em> Next</p>
since i cannot divide by 0 i will have to start from 1 and to the least number. this is because the HCF cannot be greater than any of the numbers. if you remember we stored the least number into variable 'TEMP'.
to compare the numbers we will use an if statement.
for this task we will also be using a special operator called the modulus operator
this returns the remainder from a division
its syntax is
(number) mod (divisor)
in other programming languages ,i.e. C++, mod may be replaced by the percentage sign '%'
so for our program we write
<p>For i = 1 To TEMP Step 1</p><p> If ((firstNum Mod i = 0) And (secondNum Mod i = 0)) Then</p><p> HCF = i End If Next</p>
we store the numbers into variable "HCF"
every time a greater variable is found HCF is overwritten
if i if a factor of both numbers then it is stored in to variable HCF
Step 5: Displaying Output
to display output on console screen, we use the command "console.write()" or "console.writeline()"
an important rule of the thumb is that written words must be enclose in apostrophes (""). Variables do not need to be enclosed in apostrophes
we can also make use of " & " operator to join lines remember to place a space on either sides of the & symbol
thus the program goes
<p>Console.WriteLine("The highest common factor is " & HCF)</p>
Alas the computer usually doesn't wait for the user unless told. so we add another line of program to allow for the user to read the result.
<pre><p>Console.WriteLine("PRESS ANY BUTTON TO EXIT")</p><p>Console.ReadKey()</p> <br>
Step 6: For Ease
this is my version of the programming with comments for aid.
'in any program we must declare variables Dim firstNum, secondNum, TEMP, HCF As Integer ' "As Integer" symbolizes that the nature of data for these variables are integers
'first we inform the user about the instructions Console.WriteLine("enter two numbers for highest common factor") 'then we prompt the user to enter a number Console.WriteLine("enter first number") 'we store the digit in to a variable firstNum firstNum = Console.ReadLine 'then we prompt the user to enter a second number Console.WriteLine("enter second number") 'similarly we store that too, but in a different variable ' we dont want the first one to be overwritten secondNum = Console.ReadLine
'we compare which one is larger and store it into a Temporary storage "TEMP" If firstNum < secondNum Then TEMP = firstNum ElseIf firstNum > secondNum Then TEMP = secondNum
'in the clause underneath we stored a value into the TEMP even though the first and second numbers were equal 'this is because we needed the "highest" number of the either whatever it may be.
ElseIf firstNum = secondNum Then TEMP = secondNum End If
'here is where the programming really begins 'the mod function divides the integer by a number and returns the remainder 'this is useful, in this way we can check by which numbers are the remainders zero
'here we use a "FOR ITERATION LOOP" to do the job 'we create a variable 'i' and increase it by 1 after every loop
For i = 1 To TEMP Step 1 '"Step 1" shows that there is an increment of 1 after every loop
'as you can see we also used an AND function 'this is because we only needed numbers which divides both variables giving remainder zero
'another important note is that we cannot begin i at 0 'this is because anything divided by 0 may lead to infinity If ((firstNum Mod i = 0) And (secondNum Mod i = 0)) Then
'we store the numbers into variable "HCF" ' every time a greater variable is found HCF is overwritten HCF = i End If Next
Console.Clear() ' this command clears anything written on the console screen
Console.WriteLine("highest common factor = " & HCF) 'this command displays message on console screen
'the commands underneath allows for exiting the console screen Console.WriteLine() Console.WriteLine("PRESS ANY BUTTON TO EXIT") Console.ReadKey()
'P.S 'while programming, as long as you do not ruin the syntaxes ' you are free to put spaces, tabs or empty lines to make the program look less messy