Q:

Program to print the permutation (nPr) of the given number

belongs to collection: Number Programs

0

Permutation

It 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

  • STEP 1: START
  • STEP 2: DEFINE n, r, per, fact1, fact2
  • STEP 3: PRINT n, r
  • STEP 4: fact1 =n
  • STEP 5: REPEAT STEP 6 UNTIL i>=1
  • STEP 6: fact1 = fact1*i
  • STEP 7: DEFINE number
  • STEP 8: SET number = n - r
  • STEP 9: fact 2 = fact2*i
  • STEP 10: SET per = fact1/fact2
  • STEP 11: PRINT per
  • STEP 12: END

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to print the permutation (nPr) of the give... >>
<< Program to print the first 10 prime numbers Prime ...