![]() |
|
|
#1 (permalink) |
|
Registered User
![]() Join Date: Apr 2004
Location: NYC
Rep Power: 1186954
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
I need to create a method that can simplify a fraction, that is, represent it as a fraction where the numerator and the denominator have no common divisors. it should read in the numerator and the denominator and then print out the fraction in its simplified form.
since dividing the numerator and denominator by their gcd will simplify the fraction. here is a sample run (user input) enter a numerator: 6 enter a denominator: 8 your fraction is 6/8 the simplified form is 3/4 I wrote this code and it compiles and makes sense to me at least. However, it doesn't work properly for more difficult integers like 20, 30. Can anyone spot the problem. nothing is wrong with the compiling, just doesn't work logically. I can't use the Euclidean Algorithm, so I basically had to write the simplest program to find gcd. public static int gcd(int a, int b) { int gcd; if (a > b && a % b == 0) { gcd = b; return gcd; } else if (b > a && b % a == 0) { gcd = a; return gcd; } else if (a > b && a % b != 0) { int count = b; do { count = count - 1; } while (b % count != 0 && a % count != 0); gcd = count; return gcd; } else if (b > a && b % a != 0) { int count = a; do { count = count - 1; } while (b % count != 0 && a % count != 0); gcd = count; return gcd; } else { gcd = a; return gcd; } }
__________________
Hoes on my dick, like Prim' you the best baby... |
|
|
|
|
|
#2 (permalink) |
|
Registered User
![]() Join Date: Jun 2005
Location: somewhere shaaa...
Rep Power: 0
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
i think your problem is your structure. too many if-elses and whiles in the same block of code is usually a sign of trouble.
why don't you consider doing it like this: create an array of factors for each number, then go through and compare both arrays. select the largest common factor, and divide both by the number, then present your answer? that should take like only 3 while/for loops. let me know if you want further explanations. also, it may be better to use matlab for this, unless you absolutely must use java. |
|
|
|
|
|
#4 (permalink) |
|
goat herder
![]() Join Date: Apr 2002
Location: in love
Rep Power: 21474910
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
ok if u have n/d,
write a while i > 0, } if n%a = 0 && d%a = 0 then display n/a '/' d/a else i-1 } basic data structure. thats more for c/c++ sha.
__________________
Last edited by COMMONCENTS; 07-17-2007 at 11:36 PM.. |
|
|
|
|
|
#6 (permalink) | |
|
Registered User
![]() Join Date: Apr 2004
Location: NYC
Rep Power: 1186954
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Quote:
__________________
Hoes on my dick, like Prim' you the best baby... |
|
|
|
|
|
|
#7 (permalink) | |
|
Registered User
![]() Join Date: Jun 2003
Location: waterside
Rep Power: 21474866
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Quote:
__________________
It is the mark of an educated mind to be able to entertain a thought without accepting it. - Aristotle |
|
|
|
|
|
|
#8 (permalink) | |
|
goat herder
![]() Join Date: Apr 2002
Location: in love
Rep Power: 21474910
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Quote:
if i was to write i'd use c++, and i's only need one while loop and one if statement.
__________________
|
|
|
|
|
|
|
#10 (permalink) | |
|
Registered User
![]() Join Date: Jun 2005
Location: somewhere shaaa...
Rep Power: 0
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Quote:
see dis kopykata even if that works, that would only be the first part. maybe you should say what your logic is. your code isn't clear at all. |
|
|
|
|
|
|
#12 (permalink) |
|
Registered User
![]() Join Date: Apr 2004
Location: NYC
Rep Power: 1186954
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
i seriously need help, make una help and stop making fun of my condition. I don loose sleep for 2 night becos of this nonsense
__________________
Hoes on my dick, like Prim' you the best baby... |
|
|
|
|
|
#15 (permalink) |
|
Registered User
![]() Join Date: Apr 2004
Location: NYC
Rep Power: 1186954
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
^it still no dey work. i don try many ways, but it still dey give me errorrs. please work with me, compile and run it and help me figure where the problem is
__________________
Hoes on my dick, like Prim' you the best baby... |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|