Permutation And Combination
The Java code below takes input from the user to return permutation and combination.
import java.util.Scanner;
public class Math_programme {
static int factorial(int a){
if (a==0){
return 1;
}
else if (a==1){
return 1;
}
return a*factorial(a-1);
}
static int permutation(int a,int b){
return factorial(a)/(factorial(a-b));
}
static int combination(int a,int b){
return factorial(a)/(factorial(b)*factorial(a-b));
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
String d=sc.next();
int b=sc.nextInt();
int c;
int minus=a-b;
if (d.equals("p") && minus>=0){
c=permutation(a,b);
System.out.println(c);
}
else if (d.equals("c") && minus>=0){
c=combination(a,b);
System.out.println(c);
}
else {
System.out.println("Wrong format");
}
}
}