# Java Program to Calculate nPr

In the previous article, we have seen Java Program to Calculate nCr

In this article we will see how to calculate nPr using Java programming language.

## Java Program to Calculate nPr

Before jumping into the program let’s know about nPr.

nPr:

nPr is the probability function which represents permutations.

nPr is the selection of objects from group of objects with caring about order of objects means in case of nPr order of objects matters.

nPr = n!/(n-r)!

Where, selecting r objects from n number of objects.

Let’s see different ways to find future investment value.

### Method-1: Java Program to Calculate nPr By Using Static Input Value

Approach:

• Declare the value of n and r value.
• Check if n value is greater than r value or not if yes then call findPermutation() method.
• Else nPr value can not be found out as n value is less than r.
• Declare a method factorial() to find factorial of a number.
• Inside findPermutation() method using the formula find nPr value.

Program:

import java.util.*;

class Main
{
public static void main(String arg[])
{
//Scanner class object created
Scanner sc=new Scanner(System.in);
//n value declared
int n=11;
//r value declared
int r=6;
//checking n is greater than equal to r or not
if(n>=r)
{
System.out.println("Value of "+n+"p"+r+" : "+ findPermutation(n,r));
}
else
{
System.out.println("r value can not be more than n value");
}

}

//factorial() method to find factorial of a number
public static double factorial(double n)
{
int i=1;
double fact=1;
while(i<=n)
{
fact=fact*i;
i++;
}
return fact;
}

//findPermutation() method to find permutations i.e. nPr value
public static double findPermutation(int n, int r)
{
//finding nPr value
double nPr=factorial(n)/factorial(n-r);
//returning nPr value
return nPr;
}
}
Output:
Value of 11p6 : 332640.0

### Method-2: Java Program to Calculate nPr By Using User Input Value

Approach:

• Take input of n and r value.
• Check if n value is greater than r value or not if yes then call findPermutation() method.
• Else nPr value can not be found out as n value is less than r.
• Declare a method factorial() to find factorial of a number.
• Inside findPermutation() method using the formula find nPr value.

Program:

import java.util.*;

class Main
{
public static void main(String arg[])
{
//Scanner class object created
Scanner sc=new Scanner(System.in);
//taking input of n value
System.out.println("Enter n value: ");
int n=sc.nextInt();
//taking input of r value
System.out.println("Enter r value: ");
int r=sc.nextInt();
//checking n is greater than equal to r or not
if(n>=r)
{
System.out.println("Value of "+n+"p"+r+" : "+ findPermutation(n,r));
}
else
{
System.out.println("r value can not be more than n value");
}

}

//factorial() method to find factorial of a number
public static double factorial(double n)
{
int i=1;
double fact=1;
while(i<=n)
{
fact=fact*i;
i++;
}
return fact;
}

//findPermutation() method to find permutations i.e. nPr value
public static double findPermutation(int n, int r)
{
//finding nPr value
double nPr=factorial(n)/factorial(n-r);
//returning nPr value
return nPr;
}
}
Output:

Enter n value:
5
Enter r value:
2
Value of 5p2 : 20.0

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Programs: