Question

Algorithm 2: The common form of this algorithm replaces subtracting the small positive number from the...

Algorithm 2:
The common form of this algorithm replaces subtracting the small positive number from the big number (possibly many times) with finding the remainder in long division. This form of the algorithm also starts with a pair of positive integers, then forms a new pair consisting of the smaller number and the remainder obtained by dividing the larger number by the smaller number. The process repeats until one number is zero. The other number then is the greatest common factor of the original pair.

Explanation

ex. (int a, int b) is (1071,462)

1071,462 462(smaller number),147(remainder) 147(smaller number),21(remainder) 21(smaller number),0

GCF is 21


Write an iterative method that will perform algorithm 2.
public static int findGCF2(int a, int b)
{

}

Please solve with a for loop and perhaps modulus operators and simple java concepts.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

java code:

using iterative method:


package gcfdriver;


public class GCFDriver {

  
public static void main(String[] args) {
  
//call function and print value
System.out.println(findGCF2(1071,462));
  
}
//return GCF of 2 no
public static int findGCF2(int a, int b)
{
//if small no is not 0
if (b != 0)
return findGCF2(b, a % b); //call function with b and remainder
else
return a; //else return a
}
}

using for loop method:

public class GCFDriver {

  
public static void main(String[] args) {
  
//call function and print value
System.out.println(findGCF2(1071,462));
  
}
//return GCF of 2 no
public static int findGCF2(int a, int b)
{
int gcf=1;
for(int i = 1; i <= a && i <= b; ++i)
{
// if i is the factor of both a and b
if(a % i==0 && b % i==0)
gcf = i;
}
return gcf; //return gcf value
}
}

output:

//for any clarification please do comments. if you found this solution useful, please give me thumbs up

Add a comment
Know the answer?
Add Answer to:
Algorithm 2: The common form of this algorithm replaces subtracting the small positive number from the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below...

    First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below Include each of the following in your answer (if applicable – explain in a paragraph) Research problem: what do you want to solve using Delphi? Sample: who will participate and why? (answer in 5 -10 sentences) Round one questionnaire: include 5 hypothetical questions you would like to ask Discuss: what are possible outcomes of the findings from your study? Hint: this is the conclusion....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT