April 21, 2012

Program probabilitas


import java.util.Scanner;

class probabilitas
{
public int n, k;
Scanner input = new Scanner(System.in);

probabilitas()
{
menu();
}



public void inputData()
{
System.out.print("Masukan Nilai n : ");
n = input.nextInt();
System.out.print("Masukan Nilai k : ");
k = input.nextInt();
tanya();
}


public int fak(int n)
{
int jumlah = 1;
for(int i=1;i<=n;i++){
jumlah = jumlah * i;
}
return jumlah;
}



public void permutasi()
{
if(n<k){System.out.println("\nMATH ERROR - n<k");}
else{
System.out.println("\nP("+n+","+k+")"+"= "+(fak(n)/fak(n-k)));
}
tanya();
}



public void kombinasi()
{
if(n<k){System.out.println("\nMATH ERROR - n<k");}
else
{
//rumus kombinasi
System.out.println("\nC("+n+","+k+")"+"= "+(fak(n)/(fak(n-k)*fak(k))));
}

tanya();
}



public void tanya()
{
System.out.print("\n\nKembali ke Menu ?[Y/T] : ");
String pilih = input.next();
String IYA = "Y";
boolean jwb = pilih.equals(IYA);
if(jwb==true)
{
menu();
}
else{System.exit(0);}
}




public void menu()
{
System.out.println("==========================");
System.out.println("|  PROGRAM PROBABILITAS  |");
System.out.println("==========================");
System.out.println("| 1. Input Nilai         |");
System.out.println("| 2. Permutasi           |");
System.out.println("| 3. Kombinasi           |");
System.out.println("| 4. Exit                |");
System.out.println("==========================");
System.out.print(" Masukan pilhan Anda : ");
int pil = input.nextInt();
switch(pil){
case 1: inputData();break;
case 2: permutasi();break;
case 3: kombinasi();break;
case 4: System.exit(0);
default: menu();
}
}

public static void main(String[] args){
new probabilitas();
}

}

Tidak ada komentar:

Posting Komentar