beranda

Minggu, 25 Maret 2012

Program Sederhana java Mencari Nilai FPB

package fpb;
import java.io.*;
public class java {
    static String input;
    static int bil_1, bil_2;
    static DataInputStream in = new DataInputStream(System.in);
    public static int recurseFPB(int m, int n){
       if(m == 0)
           return n;
       else if (m<n)
           return recurseFPB(n,m);
       else
           return recurseFPB(m % n,n );
    }
    public static void main(String[] args) {
        try{// TODO code application logic here
            System.out.println("\nMenghitung FPB dari tiga Bilangan");
            System.out.print("masukkan bilangan ke-1: ");
            input = in.readLine();
            bil_1 = Integer.parseInt(input);

            System.out.print("masukkan bilangan ke-2: ");
            input = in.readLine();
            bil_2 = Integer.parseInt(input);

        }
        catch(Exception e){}
        recurseFPB(bil_1, bil_2);
        System.out.println();
        System.out.print("FPB dari " +bil_1+ " dan  " +bil_2+ "adalah ");
        System.out.print(recurseFPB(bil_1,bil_2));
    }
}

Tidak ada komentar:

Posting Komentar