hey yall,
For class i have to create a program that reduces a fraction that the user inputs. This has stumped me mostly because i have the most trouble reducing fractions. I can reduce fraction but i do them mostly in my head and i dont know step by step on how to do so. but here is my code. (pleese forgive me i do not comment like i should and it is still in development so it is messy and unorganized)
CODE
Public Class Form1
Private Sub btnReduce_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReduce.Click
Dim Numerator, Denominator, GCF As Integer
Dim answer As String
Numerator = Reduce_Numerator(Numerator, Denominator, GCF)
Denominator = Reduce_Denominator(Numerator, Denominator, GCF)
answer = String.Concat(Val(Me.txtNumerator.Text) & "/" & Val(Me.txtDenominator.Text) & "Reduces To:" & Numerator & "/" & Denominator)
Me.lblMessage.Text = answer
End Sub
Function Reduce_Numerator(ByVal Numerator As Integer, ByVal Denominator As Integer, ByVal GCF As Integer) As Integer
Numerator = Val(Me.txtNumerator.Text)
Denominator = Val(Me.txtDenominator.Text)
GCF = Factor(Numerator, Denominator, GCF)
Numerator = Numerator / GCF
Denominator = Denominator / GCF
Return Numerator
End Function
Function Reduce_Denominator(ByVal Numerator As Integer, ByVal denominator As Integer, ByVal GCF As Integer) As Integer
GCF = Factor(Numerator, denominator, GCF)
Numerator = Val(Me.txtNumerator.Text)
denominator = Val(Me.txtDenominator.Text)
Numerator = GCF / Numerator
denominator = GCF / denominator
Return denominator
End Function
Function Factor(ByVal numerator As Integer, ByVal denominator As Integer, ByVal GCF As Integer) As Integer
Dim max_num As Integer
If numerator > denominator Then
max_num = denominator
Else
max_num = numerator
End If
numerator = Val(Me.txtNumerator.Text)
denominator = Val(Me.txtDenominator.Text)
If numerator Mod denominator = 0 Then
GCF = numerator
ElseIf denominator Mod numerator = 0 Then
GCF = denominator
Else
Do While GCF Mod numerator And GCF Mod denominator
GCF -= 1
Loop
End If
Return GCF
End Function
End Class
thanks
~xtreampb