October 16, 2008 thumb GCD = FPB

GCD atau FPB adalah faktor persekutuan terbesar dari dua angka atau lebih. berikut ini source code untuk mencari sebuah FPB dari dua buah angka…..

/*
This class implement of GCD
We recommended you to using Jcreator pro v3.0
Author : huda
e-mail : huda890@gmail.com
*/
import java.io.*;
class gcd
{
public static void main(String args[])throws IOException
{
String temporary;
/*first number*/
int first_number;
/*second number*/
int second_number;
/*build object of gcd class*/
gcd number=new gcd();
/*build for input from konsole*/
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“GCD”);
System.out.print(“insert first number : “);
/*read string from console*/
temporary=input.readLine();
/*convert from temporary to integer and save to first_number*/
first_number=Integer.parseInt(temporary);
System.out.print(“insert second number : “);
/*read string from console*/
temporary=input.readLine();
/*convert from temporary to integer and save to first_number*/
second_number=Integer.parseInt(temporary);
/*choose the proses*/
if(first_number>second_number)
number.proses(second_number,first_number);
/*if second number more less than first number*/
else
number.proses(first_number,second_number);
/*if second number more big than first number*/
}
/*the methode for found the gcd*/
int proses(int first,int second)
{
/*pivot is the temporary variable */
int pivot;
if(first!=0)
{
pivot=second;
second=first;
first=pivot%first;
/*recurtion function*/
return(proses(first,second));
}
/*print the gcd to console*/
System.out.println(“GCD : “+second);
return(0);
}
}

Comments

total comments