Hello,
I have a problem. I am a beginner in java.
How should I make an algorithm for finding the max common divisor between 2 numbers?
I mean, how the code goes and why is it coded like that.
Thanks!
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hello,
I have a problem. I am a beginner in java.
How should I make an algorithm for finding the max common divisor between 2 numbers?
I mean, how the code goes and why is it coded like that.
Thanks!
Last edited by AdriCORD.97; October 1st, 2020 at 02:29 PM.
Can you post the details of the algorithm that you are trying to write code for? Once there is a good algorithm, then work on writing the code for it.
If you don't understand my answer, don't ignore it, ask a question.
public class GreatestCommonDivisor {
public static void main(String[] args) {
System.out.println(getGreatestCommonDivisor(20,10) );
}
public static int getGreatestCommonDivisor(int first, int second){
if(first < 10 || second < 10){
return -1;
}
int divisor = 1;
if(first > second){
divisor = first % second;
}
if(second > first){
divisor = second % first;
}
return divisor;
}
}
The exercise in the course i'm studying says as a tip to use a while or for loop, and that is the problem. I've read other threads about greatest common divisors and in their code demonstrations the loops have an array keyword. I don't know about such thing. The professor never teached about arrays or other keywords like that, so I'm lost. You can see there that the if statements where the remainder operators are are wrong, since 20 % 10 is 0 and 0 is not a divisor. Would you please take your time to explain arrays to me, or is it too complicated for them to be explained here?
Where is the description of the algorithm that you are trying to write the code for?
It is always a bad idea to start by writing code before you have a working algorithm.
http://www.java-forums.org/misc.php?do=bbcode#code
Please edit your post and wrap your code with code tags:
[code]
**YOUR CODE GOES HERE**
[/code]
to get highlighting and preserve formatting.
If you don't understand my answer, don't ignore it, ask a question.
public class GreatestCommonDivisor { public static void main(String[] args) { System.out.println(getGreatestCommonDivisor(20,10)); } public static int getGreatestCommonDivisor(int first, int second){ if(first < 10 || second < 10){ return -1; } int divisor = 1; if(first > second){ divisor = first % second; } if(second > first){ divisor = second % first; } return divisor; } }
--- Update ---
What do you mean by writing code before having a working algorithm? I'm sorry, I'm new to this
An algorithm is a sort of blueprint of what the code will do. No one builds a house or a car without a blueprint describing what the end product looks like. The same with a computer program. Don't try writing the code until there is a design for how the code should work to solve the problem.
What is the purpose of testing if first or second is less than 10?
What the purpose of this statement: divisor = first % second;
If you don't understand my answer, don't ignore it, ask a question.
The exercise says that the numbers should be equal or greater than 10.
And the statement: divisor = first % second; is using an algorithm I read in wikipedia. It is wrong I see.
I'll work on an algorithm now and consult later if I need to. Thank you!