VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Simplifies a fraction when you give numerator and denominator if it can be simplified...

by RedSting71 (9 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Sun 30th January 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Simplifies a fraction when you give numerator and denominator if it can be simplified...

Rate Simplifies a fraction when you give numerator and denominator if it can be simplified...



--------------------------------------------------------------------------------
Public Function Simplify(numerator as double, denominator as double) as string
n = numerator
d = denominator
x = Math_GCF(Val(n), Val(d))
n = n / Val(x)
d = d / Val(x)
Simplify = Trim(str(n)) + "/" + Trim(str(d))
End Function
--------------------------------------------------------------------------------
Public Function Math_GCF(number1 as double, number2 as double) as double
If number1 > number2 Then a = number2
If number2 > number1 Then a = number1
If number1 = number2 Then a = number1
n1 = number1: n2 = number2
h = n2 / n1
h2 = n1 / n2
If n1 > n2 Then
    If Math_IsInteger(Val(h2)) Then Math_GCF = n2: Exit Function
End If
If n2 > n1 Then
    If Math_IsInteger(Val(h)) Then Math_GCF = n1: Exit Function
End If
y = Math_LCM(number1, number2)
y = Val(y)
x = 1
Do: DoEvents
y = Math_LCM(Val(n1), Val(n2)) / Val(x)
b = n1 / x
c = n2 / x
If Math_IsInteger(Val(y)) Then
    If Math_IsInteger(Val(b)) Then
        If Math_IsInteger(Val(c)) Then
            GCF = x
        End If
    End If
End If
x = x + 1
Loop Until x = a
Math_GCF = GCF
If n1 > n2 Then
    Y1 = Math_LCM(Val(n1), Val(n2))
    Y1 = Val(Y1) / Val(n2)
    b = n1 / n2
    c = n2 / n2
    If Math_IsInteger(Val(Y1)) Then
        If Math_IsInteger(Val(b)) Then
            If Math_IsInteger(Val(c)) Then
                GCF = n2
            End If
        End If
    End If
End If
If n1 < n2 Then
    Y1 = Math_LCM(Val(n1), Val(n2))
    Y1 = Val(Y1) / Val(n1)
    b = n1 / n1
    c = n2 / n1
    If Math_IsInteger(Val(Y1)) Then
        If Math_IsInteger(Val(b)) Then
            If Math_IsInteger(Val(c)) Then
                GCF = n1
            End If
        End If
    End If
End If
Math_GCF = GCF
End Function
--------------------------------------------------------------------------------
Public Function Math_IsInteger(number as double) as boolean
n1 = Trim(str(number))
n = InStr(1, n1, ".")
If n = 0 Then Math_IsInteger = True
If n > 0 Then Math_IsInteger = False
End Function



Download this snippet    Add to My Saved Code

Simplifies a fraction when you give numerator and denominator if it can be simplified... Comments

No comments have been posted about Simplifies a fraction when you give numerator and denominator if it can be simplified.... Why not be the first to post a comment about Simplifies a fraction when you give numerator and denominator if it can be simplified....

Post your comment

Subject:
Message:
0/1000 characters