473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Reducing a fraction

Hi again all,

I still am haaving trouble trying to get a least common denominator. I don't nessicarrily want someone to do the code just give me some direction on how to write it. I have included the code I have so far for the button click it works fine except that it will not do the reduction of the fraction answer. This is from a button click on a VB form.
Expand|Select|Wrap|Line Numbers
  1.  Private Sub AddFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFract.Click
  2.         ' setting up my variables
  3.         Dim num1add As Double = AddNum1.Text
  4.         Dim num2add As Double = AddNum2.Text
  5.         Dim den1add As Double = AddDen1.Text
  6.         Dim den2add As Double = AddDen2.Text
  7.         Dim numansadd As Double
  8.         Dim denansadd As Double
  9.         Dim WholeNumberadd As Double
  10.  
  11.         ' if denominators  are different in text boxes then run this
  12.         If den1add <> den2add Then
  13.  
  14.             'creates the numerator of the answer
  15.             numansadd = ((num1add * den2add) + (num2add * den1add))
  16.             'creates the denominator of the answer
  17.             denansadd = (den1add * den2add)
  18.  
  19.             ' if denominators match run this
  20.         Else
  21.             ' adds the 2 numerators to create an answer
  22.             numansadd = (num1add + num2add)
  23.             ' keeps the matching denominators
  24.             denansadd = (den1add)
  25.  
  26.         End If
  27.         ' after adding if the numerator is higher that the denominatoer run this
  28.         If numansadd >= denansadd Then
  29.  
  30.             ' setting variables
  31.             Dim LoopCountadd As Integer
  32.             Dim TimesLoopedadd As Integer
  33.             TimesLoopedadd = 0
  34.             LoopCountadd = 0
  35.  
  36.             ' a loop to subtract the denominator from the numerator until the numerator_
  37.             ' is smaller than the denominator
  38.  
  39.             Do While numansadd >= denansadd
  40.                 ' subtracts the numerator from the denominator
  41.                 TimesLoopedadd = (denansadd - numansadd)
  42.                 ' counts up 1 for every time the numerator is subtracted from the denominator
  43.                 LoopCountadd += 1
  44.                 ' makes WholeNumber = the total of times the loop occurred
  45.                 WholeNumberadd = LoopCountadd
  46.                 ' makes the remainder = the new numerator answer
  47.                 numansadd = (numansadd - denansadd)
  48.  
  49.                 ' goes back to see if the numerator is higher than the denominator
  50.             Loop
  51.            ' "this is where I want it to reduce"
  52.         End If
  53.         ' if the new numerator = 0 only the whole number is displayed
  54.         If numansadd = 0 Then
  55.  
  56.             AddFractAns1.Text = "" & (WholeNumberadd)
  57.  
  58.             ' otherwise the whole number and the remaining fraction are displayed
  59.         Else
  60.  
  61.             AddFractAns1.Text = "" & (WholeNumberadd) & " and " _
  62.             & (numansadd) & "/" & (denansadd)
  63.  
  64.         End If
  65.  
  66.     End Sub
  67.  
FYI this is just a project to teach myself VB. Thats why i have all the notes in my code
Thank you very much in advance for any help.
Nov 20 '07 #1
4 1616
Shashi Sadasivan
1,435 Expert 1GB
Hi,
So this generally dosent seem to be the LCD issue.
You are bypassing the LCD issue by multiplying the denominators.

What you are really after is how to reduce the fraction!

A potential mistake would be if a fractions numberator is greater than the denominator.
what is the numbrator is a prime number and the denominator is smaller than that? --- infinite loop

what you could do is find all the prime number contained in numerator and denominator, and divide them by the common prime numbers. Repeat this until there are no more prime numbers left in it.

I have a DLL made which will return a List of prime numbers. Contact me if you want to use that.

cheers

EDIT: take a look at this site, explains in different ways how to get the LCD and reduce fractions. You may choose any of the procedures.
Nov 20 '07 #2
Hi,
So this generally dosent seem to be the LCD issue.
You are bypassing the LCD issue by multiplying the denominators.

What you are really after is how to reduce the fraction!

A potential mistake would be if a fractions numberator is greater than the denominator.
what is the numbrator is a prime number and the denominator is smaller than that? --- infinite loop

what you could do is find all the prime number contained in numerator and denominator, and divide them by the common prime numbers. Repeat this until there are no more prime numbers left in it.

I have a DLL made which will return a List of prime numbers. Contact me if you want to use that.

cheers

EDIT: take a look at this site, explains in different ways how to get the LCD and reduce fractions. You may choose any of the procedures.
oops DOH! your right I meant reduce the fraction ( Embarassed)

I found something called Euclid's algorithm I tried that and your right I did wind up getting an infinite answer. What i cant undersatand is why I get this when i wait until the fraction has allready been split into a whole # and a fraction that has a lower numerator than denominator.

here's how I adjusted the code to try to do what I wanted. it was originally a function. And I have not got functions down yet :)
Expand|Select|Wrap|Line Numbers
  1.  Dim x As Integer
  2.         Dim y As Integer
  3.         Dim temp As Integer
  4.         Dim GCD As Integer
  5.  
  6.         x = Math.Abs(x)
  7.         y = Math.Abs(y)
  8.  
  9.         Do While (y <> 0)
  10.             temp = x Mod y
  11.             x = y
  12.             y = temp
  13.         Loop
  14.         GCD = x
  15.  
  16.  
  17.         x = x / GCD
  18.         y = y / GCD
  19.         denansadd = y
  20.         numansadd = x
  21.  
any ideas?
Nov 21 '07 #3
Hi,
So this generally dosent seem to be the LCD issue.
You are bypassing the LCD issue by multiplying the denominators.

What you are really after is how to reduce the fraction!

A potential mistake would be if a fractions numberator is greater than the denominator.
what is the numbrator is a prime number and the denominator is smaller than that? --- infinite loop

what you could do is find all the prime number contained in numerator and denominator, and divide them by the common prime numbers. Repeat this until there are no more prime numbers left in it.

I have a DLL made which will return a List of prime numbers. Contact me if you want to use that.

cheers

EDIT: take a look at this site, explains in different ways how to get the LCD and reduce fractions. You may choose any of the procedures.
I finished it finally thanks again Shashi. Here's what I finally came up with and it works perfectly. tho probably over engeneered :)

Function.

Expand|Select|Wrap|Line Numbers
  1. ' Returns the greatest common divisor using Euclid's algorithm
  2.     Private Function GCD(ByVal x As Integer, ByVal y As Integer) As Integer
  3.         Dim temp As Integer
  4.  
  5.         x = Math.Abs(x)
  6.         y = Math.Abs(y)
  7.  
  8.         Do While (y <> 0)
  9.             temp = x Mod y
  10.             x = y
  11.             y = temp
  12.         Loop
  13.  
  14.         Return x
  15.     End Function
  16.  
  17. Addition
  18.  
Expand|Select|Wrap|Line Numbers
  1. ' Setting up my variables
  2.         Dim num1add As Integer = AddNum1.Text
  3.         Dim num2add As Integer = AddNum2.Text
  4.         Dim den1add As Integer = AddDen1.Text
  5.         Dim den2add As Integer = AddDen2.Text
  6.         Dim numansadd As Integer
  7.         Dim denansadd As Integer
  8.         Dim WholeNumberAdd As Integer
  9.         Dim ReduceFraction As Integer
  10.  
  11.         ' If denominators  are different in text boxes then run this
  12.         If den1add <> den2add Then
  13.  
  14.             'Creates the numerator of the answer
  15.             numansadd = ((num1add * den2add) + (num2add * den1add))
  16.             'Creates the denominator of the answer
  17.             denansadd = (den1add * den2add)
  18.  
  19.             ' If denominators match run this
  20.         Else
  21.             ' Adds the 2 numerators to create an answer
  22.             numansadd = (num1add + num2add)
  23.             ' Keeps the matching denominators
  24.             denansadd = (den1add)
  25.  
  26.         End If
  27.         ' After adding if the numerator is higher that the denominatoer run this
  28.         If numansadd >= denansadd Then
  29.  
  30.             ' Setting variables
  31.             Dim LoopCountadd As Integer
  32.             Dim TimesLoopedadd As Integer
  33.             TimesLoopedadd = 0
  34.             LoopCountadd = 0
  35.  
  36.             ' A loop to subtract the denominator from the numerator until the numerator_
  37.             ' is smaller than the denominator
  38.  
  39.             Do While numansadd >= denansadd
  40.                 ' Subtracts the numerator from the denominator
  41.                 TimesLoopedadd = (denansadd - numansadd)
  42.                 ' Counts up 1 for every time the numerator is subtracted from the denominator
  43.                 LoopCountadd += 1
  44.                 ' Makes WholeNumber = the total of times the loop occurred
  45.                 WholeNumberAdd = LoopCountadd
  46.                 ' Makes the remainder = the new numerator answer
  47.                 numansadd = (numansadd - denansadd)
  48.  
  49.                 ' Goes back to see if the numerator is higher than the denominator
  50.             Loop
  51.  
  52.         End If
  53.  
  54.         ' Using the function GCD to get the greatest common divisor _
  55.         ' With numansadd and denansadd as the variables
  56.         ReduceFraction = GCD(numansadd, denansadd)
  57.         ' Divides the numerator by the greatest common divisor
  58.         numansadd = (numansadd / ReduceFraction)
  59.         ' Divides the denominator by the greatest common divisor
  60.         denansadd = (denansadd / ReduceFraction)
  61.  
  62.         ' If the new numerator = 0 only the whole number is displayed
  63.         If numansadd = 0 Then
  64.  
  65.             AddFractAns1.Text = "" & (WholeNumberAdd)
  66.  
  67.             ' If there is no whole number then display the fraction only
  68.         ElseIf WholeNumberAdd = 0 Then
  69.             AddFractAns1.Text = "" & (numansadd) & "/" & (denansadd)
  70.  
  71.             ' Otherwise the whole number and the remaining fraction are displayed
  72.         Else
  73.  
  74.             AddFractAns1.Text = "" & (WholeNumberAdd) & " and " _
  75.             & (numansadd) & "/" & (denansadd)
  76.  
  77.         End If
  78.  
subtraction

Expand|Select|Wrap|Line Numbers
  1. ' Setting up my variables
  2.         Dim num1sub As Integer = SubNum1.Text
  3.         Dim num2sub As Integer = SubNum2.Text
  4.         Dim den1sub As Integer = SubDen1.Text
  5.         Dim den2sub As Integer = SubDen2.Text
  6.         Dim numanssub As Integer
  7.         Dim denanssub As Integer
  8.         Dim WholeNumbersub As Integer
  9.         Dim ReduceFraction As Integer
  10.  
  11.         ' If denominators  are different in text boxes then run this
  12.         If den1sub <> den2sub Then
  13.  
  14.             'Creates the numerator of the answer
  15.             numanssub = ((num1sub * den2sub) - (num2sub * den1sub))
  16.             'Creates the denominator of the answer
  17.             denanssub = (den1sub * den2sub)
  18.  
  19.             ' If denominators match run this
  20.         Else
  21.             ' Adds the 2 numerators to create an answer
  22.             numanssub = (num1sub - num2sub)
  23.             ' Keeps the matching denominators
  24.             denanssub = (den1sub)
  25.  
  26.         End If
  27.         ' After the math. if the numerator is higher that the denominatoer run this
  28.         If Math.Abs(numanssub) >= Math.Abs(denanssub) Then
  29.  
  30.             ' Setting variables
  31.             Dim LoopCountadd As Integer
  32.             Dim TimesLoopedadd As Integer
  33.             TimesLoopedadd = 0
  34.             LoopCountadd = 0
  35.  
  36.             ' A loop to subtract the denominator from the numerator until the numerator_
  37.             ' is smaller than the denominator
  38.  
  39.             Do While Math.Abs(numanssub) >= Math.Abs(denanssub)
  40.                 ' Subtracts the numerator from the denominator
  41.                 TimesLoopedadd = (Math.Abs(denanssub) - Math.Abs(numanssub))
  42.                 ' Counts up 1 for every time the numerator is subtracted from the denominator
  43.                 LoopCountadd += -1
  44.                 ' Makes WholeNumber = the total of times the loop occurred
  45.                 WholeNumbersub = LoopCountadd
  46.                 ' Makes the remainder = the new numerator answer
  47.                 numanssub = (Math.Abs(numanssub) - Math.Abs(denanssub))
  48.  
  49.                 ' Goes back to see if the numerator is higher than the denominator
  50.             Loop
  51.  
  52.         End If
  53.  
  54.         ' Using the function GCD to get the greatest common divisor _
  55.         ' with numansadd and denansadd as the variables
  56.         ReduceFraction = GCD(numanssub, denanssub)
  57.         ' Divides the numerator by the greatest common divisor
  58.         numanssub = (numanssub / ReduceFraction)
  59.         ' Divides the denominator by the greatest common divisor
  60.         denanssub = (denanssub / ReduceFraction)
  61.  
  62.         ' If the new numerator = 0 only the whole number is displayed
  63.         If numanssub = 0 Then
  64.  
  65.             SubFractAns1.Text = "" & (WholeNumbersub)
  66.  
  67.             ' Of there is no whole number then display the fraction only
  68.         ElseIf WholeNumbersub = 0 Then
  69.             SubFractAns1.Text = "" & (numanssub) & "/" & (denanssub)
  70.  
  71.             ' Otherwise the whole number and the remaining fraction are displayed
  72.         Else
  73.  
  74.             SubFractAns1.Text = "" & (WholeNumbersub) & " and " _
  75.             & (numanssub) & "/" & (denanssub)
  76.  
  77.         End If
  78.  
Multiplication
Expand|Select|Wrap|Line Numbers
  1. ' Setting my variables
  2.         Dim num1multi As Integer = MultiNum1.Text
  3.         Dim num2multi As Integer = MultiNum2.Text
  4.         Dim den1multi As Integer = MultiDen1.Text
  5.         Dim den2multi As Integer = MultiDen2.Text
  6.         Dim numansmulti As Integer = (num1multi * num2multi)
  7.         Dim denansmulti As Integer = (den1multi * den2multi)
  8.         Dim ReduceFractionmulti = GCD(numansmulti, denansmulti)
  9.         Dim WholeNumberMulti As Integer
  10.  
  11.         ' This Reduces the fraction down to the least common denominator using the function GCD
  12.         numansmulti = (numansmulti / ReduceFractionmulti)
  13.         denansmulti = (denansmulti / ReduceFractionmulti)
  14.  
  15.         ' After the math. If the numerator is higher that the denominatoer run this
  16.         If Math.Abs(numansmulti) >= Math.Abs(denansmulti) Then
  17.  
  18.             ' Setting variables
  19.             Dim LoopCountadd As Integer
  20.             Dim TimesLoopedadd As Integer
  21.             TimesLoopedadd = 0
  22.             LoopCountadd = 0
  23.  
  24.             ' A loop to subtract the denominator from the numerator until the numerator_
  25.             ' is smaller than the denominator
  26.             Do While Math.Abs(numansmulti) >= Math.Abs(denansmulti)
  27.                 ' Subtracts the numerator from the denominator
  28.                 TimesLoopedadd = (Math.Abs(denansmulti) - Math.Abs(numansmulti))
  29.                 ' Counts up 1 for every time the numerator is subtracted from the denominator
  30.                 LoopCountadd += 1
  31.                 ' Makes WholeNumber = the total of times the loop occurred
  32.                 WholeNumberMulti = LoopCountadd
  33.                 ' Makes the remainder = the new numerator answer
  34.                 numansmulti = (Math.Abs(numansmulti) - Math.Abs(denansmulti))
  35.  
  36.                 ' Goes back to see if the numerator is higher than the denominator
  37.             Loop
  38.  
  39.         End If
  40.         ' If there is no fraction display only the whole number
  41.         If numansmulti = 0 Then
  42.  
  43.             MultiFractAns1.Text = "" & (WholeNumberMulti)
  44.  
  45.             ' If there is no whole number then display the fraction only
  46.         ElseIf WholeNumberMulti = 0 Then
  47.  
  48.             MultiFractAns1.Text = "" & (numansmulti) & "/" & (denansmulti)
  49.  
  50.             ' Otherwise the whole number and the remaining fraction are displayed
  51.         Else
  52.  
  53.             MultiFractAns1.Text = "" & (WholeNumberMulti) & " and " _
  54.             & (numansmulti) & "/" & (denansmulti)
  55.  
  56.         End If
  57.  
Division

Expand|Select|Wrap|Line Numbers
  1. ' Setting my variables
  2.         Dim num1div As Integer = DivNum1.Text
  3.         Dim num2div As Integer = DivNum2.Text
  4.         Dim den1div As Integer = DivDen1.Text
  5.         Dim den2div As Integer = DivDen2.Text
  6.         Dim numansdiv As Integer = (num1div * den2div)
  7.         Dim denansdiv As Integer = (num2div * den1div)
  8.         Dim WholeNumberDiv As Integer
  9.         Dim ReduceFractionDiv = GCD(numansdiv, denansdiv)
  10.  
  11.         ' This Reduces the fraction down to the least common denominator using the function GCD
  12.         numansdiv = (numansdiv / ReduceFractionDiv)
  13.         denansdiv = (denansdiv / ReduceFractionDiv)
  14.  
  15.         ' After the math. If the numerator is higher that the denominatoer run this
  16.         If Math.Abs(numansdiv) >= Math.Abs(denansdiv) Then
  17.  
  18.             ' Setting variables
  19.             Dim LoopCountadd As Integer
  20.             Dim TimesLoopedadd As Integer
  21.             TimesLoopedadd = 0
  22.             LoopCountadd = 0
  23.  
  24.             ' A loop to subtract the denominator from the numerator until the numerator_
  25.             ' is smaller than the denominator
  26.             Do While Math.Abs(numansdiv) >= Math.Abs(denansdiv)
  27.                 ' Subtracts the numerator from the denominator
  28.                 TimesLoopedadd = (Math.Abs(denansdiv) - Math.Abs(numansdiv))
  29.                 ' Counts up 1 for every time the numerator is subtracted from the denominator
  30.                 LoopCountadd += 1
  31.                 ' Makes WholeNumber = the total of times the loop occurred
  32.                 WholeNumberDiv = LoopCountadd
  33.                 ' Makes the remainder = the new numerator answer
  34.                 numansdiv = (Math.Abs(numansdiv) - Math.Abs(denansdiv))
  35.  
  36.                 ' Goes back to see if the numerator is higher than the denominator
  37.             Loop
  38.  
  39.         End If
  40.  
  41.         ' If there is no fraction display only the whole number
  42.         If numansdiv = 0 Then
  43.  
  44.             DivFractAns1.Text = "" & (WholeNumberDiv)
  45.  
  46.             ' If there is no whole number then display the fraction only
  47.         ElseIf WholeNumberDiv = 0 Then
  48.  
  49.             DivFractAns1.Text = "" & (numansdiv) & "/" & (denansdiv)
  50.  
  51.             ' Otherwise the whole number and the remaining fraction are displayed
  52.         Else
  53.  
  54.             DivFractAns1.Text = "" & (WholeNumberDiv) & " and " _
  55.             & (numansdiv) & "/" & (denansdiv)
  56.  
  57.  
  58.  
  59.         End If
  60.  
Thx all
Nov 24 '07 #4
Shashi Sadasivan
1,435 Expert 1GB
Thanks for sharing the solutioon with us

- Shashi
Nov 25 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
0
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) ...
6
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
7
by: d0ugg | last post by:
Hi, Iam writing a code for my class and I cant figure out how to solve it. I will be glad if somebody can teach me how to do something to correct it. The program is divided it 3 parts. ...
1
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp ...
2
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
4
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not...
4
by: Semajthewise | last post by:
Hi All For those of you that helped me with the questions on this... Thank you! I believe I have solved all the bugs in the code and wanted to post the final product. This is a set of code to solve...
3
by: Myxamatosis | last post by:
we're given an implementation file to base our class of Fraction off of. Input and output are in the format x/y, with x and y being ints, with the "/" character in the middle. so to create an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.