Java Permutation and Combination Program

Write a Java program to find the permutations and combinations of a given value with an example. This Java program accepts the n and r values and calculates the permutations and combinations using the math formula.

Perm = factorial(n) / factorial(n – r)

Comb = factorial(n) / (factorial(n – r) * factorial(r))

package SimpleNumberPrograms;

import java.util.Scanner;

public class Permutations {

	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);
		int n, r, NCR, NPR;
		
		System.out.print("Please Enter n Value = ");
		n = sc.nextInt();
		System.out.print("Please Enter r Value = ");
		r = sc.nextInt();
		
		NCR = fact(n)/(fact(n - r) * fact(r));
		NPR = fact(n)/fact(n - r);
		
		System.out.println("NCR Value = " + NCR);
		System.out.println("NPR Value = " + NPR);
	}
	
	public static int fact(int number) {
		if(number == 0 || number == 1) {
			return 1;
		}
		else {
			return number * fact(number - 1);
		}
	}
}
Java Permutation and Combination Program 1

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.