The great mathematician Euclid developed an algorithm to solve forthe Greatest C
ID: 3616134 • Letter: T
Question
The great mathematician Euclid developed an algorithm to solve forthe GreatestCommon Factor or GCF of two given integers.
Euclid's algorithm:
Given positive integers m and n, determine their GCF.
1. Divide m by n and find remainder r.
2. If r = 0, the answer is n, and we are done!
3. Set m = n, n = r, and go back to step 1.
Create a program (and name the file GCF.java) that will receive twointegers as command line arguments and output their GCF as
determined using Euclid’s algorithm.
Explanation / Answer
please rate - thanks import java.util.*; public class untitled{ public static void main(String []args) {int a, b,GCF; Scanner in=new Scanner(System.in); System.out.print("enter a number: "); a=in.nextInt(); System.out.print("enter another number: "); b=in.nextInt(); if(a>b) GCF=euclid(a,b); else GCF=euclid(b,a); System.out.println("GCF of " + a +" and " +b +" is: "+GCF ); } public static int euclid(int a, int b) {int r; r=a %b; while(r!=0) {a=b; b=r; r=a%b; } return b; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.