Java Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 9: Write a Java program to calculate the permutation of a given number.

Permutation.java

import java.util.Scanner;

public class Permutation
{
	// fact()- A method to calculate factorial.
	static int fact(int num)
	{
		int factorial=1;
		for(int i=2;i<=num;i++)
		{
			factorial=factorial*i;
		}
		return factorial;
	}
	
 	//main method
	public static void main(String[] args) 
	{
		Scanner s =new Scanner(System.in);
		int n,r;
		
		System.out.println("Enter the value of n");
		n=s.nextInt();
		System.out.println("Enter the value of r");
		r=s.nextInt();

		//calculating nPr = (n)!/(n-r)!
		//calling the method fact() to calculate the factorial.
				
		double permutation= fact(n)/fact(n-r);
		System.out.println(permutation);
	}
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.