...

View Full Version : Gcd? A challenge



greeninho
02-25-2012, 10:18 PM
Can someone help me re-program this greatest common divisor program to solve for prime numbers to give an output of 1 OR such that a and b output 1.

import java.util.Scanner;
public class Euclid
{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int a=getPositive("first", sc);
int b=getPositive("second", sc);
int d=Math.min(a, b);
while(true){
if(a%d==0&&b%d==0)
break;
d--;
}
System.out.printf("gcd for %d and %d is %d%n", a, b,d);
}
static int getPositive(String instruction, Scanner sc){
int number=0;
do{
System.out.printf("enter a %s positive integer", instruction);
number=sc.nextInt();
while(number<0);
return number;
}
}

Fou-Lu
02-26-2012, 05:11 PM
I posted a recursive version of GCD here: http://www.codingforums.com/showthread.php?t=249930



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum