C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to print the permutation (nPr) of the given number
 
 PermutationIt is an ordered-arrangement/combination of a set of things or collection of objects. For example, we have a set of letters A, B, and C.....describing permutations as n distinct objects taken r at a time. Permutation of a list of n elements: n!= n (n-1)(n-2)(n-3)....3.2.1 nPr = n!/ (n-r)! =n(n-1) (n-2)(n-3).....(n-r+1) Algorithm
 Java Program
import java.util.*;
	class Program
	{
  	  public static void main(String[] args)
        {
            int n, r, per, fact1, fact2;
            Scanner sc =  new Scanner(System.in);
            System.out.println("Enter the Value of n and r?");
            n = sc.nextInt();
            r = sc.nextInt();
            fact1 = n;
            for (int i = n - 1; i >= 1; i--)
            {
                fact1 = fact1 * i;
            }
            int number;
            number = n - r;
            fact2 = number;
            for (int i = number - 1; i >= 1; i--)
            {
                fact2 = fact2 * i;
            }
            per = fact1 / fact2;
            System.out.println("nPr = "+per);
    }
}
Output: Enter the Value of n and r? 5 2 nPr = 20 C program
#include<stdio.h>
void main ()
{
    int n, r, per, fact1, fact2,number,i;
    printf("Enter the Value of n and r?");
    scanf("%d %d",&n,&r);
    fact1 = n;
    for (int i = n - 1; i >= 1; i--)
    {
        fact1 = fact1 * i;
    }
    number = n - r;
    fact2 = number;
    for (i = number - 1; i >= 1; i--)
    {
        fact2 = fact2 * i;
    }
    per = fact1 / fact2;
    printf("nPr = %d",per);
    
}
Output: Enter the Value of n and r? 5 2 nPr = 20 C# Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Permutation
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int n, r, per, fact1, fact2;
            Console.WriteLine("Enter the Value of n and r?");
            n = Convert.ToInt32(Console.ReadLine());
            r = Convert.ToInt32(Console.ReadLine());
            fact1 = n;
            for (int i = n - 1; i >= 1; i--)
            {
                fact1 = fact1 * i;
            }
            int number;
            number = n - r;
            fact2 = number;
            for (int i = number - 1; i >= 1; i--)
            {
                fact2 = fact2 * i;
            }
            per = fact1 / fact2;
            Console.WriteLine("nPr = "+per);
            Console.ReadLine();
        }
    }
}
Output: Enter the Value of n and r? 5 2 nPr = 20 Python Program
import math;
nval = int(input("Enter value of n: "));
rval = int(input("Enter value of r: "));
npr = math.factorial(n)/math.factorial(n-r);
print("nPr =",npr);
Output: Enter the value of n: 5 Enter the value of r: 2 nPr = 20 
Next Topic#
 
 |