PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Loops

Program to calculate permutation

Input - Output

Input
Enter value of n and r : 5   2

Output
permutaion : 20

Input
Enter value of n and r : 6   3

Output
permutaion : 120

Algorithm

Step 1: START

Step 2: Enter 'n' and 'r'as input.

Step 3: calculate n! and (n-r)! separately

use formula of permutaion npr=n!/(n-r)!

Step 5:print permutaion .

Step 6: END

CODE


/* Program to calculate permutaion */

#include<stdio.h>
long permutaion(int n, int r);
long factorial(int num);

int main()
{
      int n,r;
      printf(" Enter the value of n : ");
      scanf("%d",&n);
      printf(" Enter the value of r : ");
      scanf("%d",&r);
      printf(" Permutaion is : %ld",permutaion(n,r));
    return 0;
}

 long permutaion(int n,int r)
 {
      return factorial(n) / factorial(n-r);
 }

 long combination(int n,int r)
 {
      return permutaion(n,r) / factorial(r);
 }

 long factorial(int num)
 {
      long long fact =1;
      while(num > 0)
       {
        fact = fact * num;
        num--;
       }
      return fact;
 }


/* Program to calculate permutaion */

#include<iostream>
using namespace std;
long permutaion(int n, int r);
long factorial(int num);

int main()
{
      int n,r;
      cout<<" Enter the value of n : ";
      cin>>n;
      cout<<" Enter the value of r : ";
      cin>>r;
      cout<<" Permutation is : "<<permutaion(n,r);
    return 0;
}

 long permutaion(int n,int r)
 {
      return factorial(n) / factorial(n-r);
 }

 long combination(int n,int r)
 {
      return permutaion(n,r) / factorial(r);
 }

 long factorial(int num)
 {
      long long fact =1;
      while(num > 0)
       {
        fact = fact * num;
        num--;
       }
      return fact;
 }

OutPut

Enter the value of n : 5
Enter the value of r : 2

Permutaion is = 20