TheDeveloperBlog.com

Home | Contact Us

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

Program to print the permutation (nPr) of the given number on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

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

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

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#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf