C Program To Find nPr Factorial

Lets write a C Program to Find nPr Factorial for the user input values of n and r.

In this video tutorial we are showing iterative logic, recursive logic and how to write single line of code for getting factorial using recursion and ternary/conditional operator.

Formula To Calculate nPr Factorial

nPr = n! / (n-r)!;

Definition:

The number of possibilities for choosing an ordered set of r objects(a permutation) from a total of n objects.

Very Important Note: n value should always be greater than value of r. Because, for example, we can get 2 out of 6. But we can’t get 6 out of 2. i.e., the main set should always be greater than the subset.

Example:

Lets assume that there are 3 people in a park. Let their names be A, B and C. There are only 2 seats available to sit. So the possible ways for people to sit over the seat using permutation are {AB, AC, BA, BC, CA, CB}. In our example, we have 3 people, so n = 3. And we have 2 seats available, so r = 2. Finally we get 6 objects/permutation in a set, so nPr is 6, when n = 3 and r = 2.

Factorial Definition: Factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Important Note: By convention, Factorial of 0 is 1. i.e., 0! = 1.

Example: 5! = 5 x 4 x 3 x 2 x 1 which is equal to 120. i.e., 5! = 120.

Related Read:
C Program To Find nCr Factorial

Video Tutorial: C Program To Find nPr Factorial


[youtube https://www.youtube.com/watch?v=KPPHJ8yZRdk]

YouTube Link: https://www.youtube.com/watch?v=KPPHJ8yZRdk [Watch the Video In Full Screen.]

Source Code: C Program To Find nPr Factorial

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float npr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    npr = factorial(n) / factorial(n - r);

    printf("\nnPr Factorial of %d and %d is %0.2f.\n", n, r, npr);

    return 0;
}

int factorial(int num)
{
    int fact = 1, count;

    for(count = 1; count <= num; count++)
    {
        fact = fact * count;
    }
    return(fact);
}

Output 1:
Enter positive integer value for n and r
3
2

nPr Factorial of 3 and 2 is 6.00

Output 2:
Enter positive integer value for n and r
6
2

nPr Factorial of 6 and 2 is 30.00

Source Code: C Program To Find nPr Factorial using Recursion

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float npr;

    printf("Enter positive integer value for n and r\n");
    scanf("%d%d", &n, &r);

    npr = factorial(n) / factorial(n-r);

    printf("\nnPr Factorial of %d and %d is %0.2f\n", n, r, npr);

    return 0;
}

int factorial(int num)
{
    if(num)
        return(num * factorial(num - 1));
    else
        return 1;
}

Output 1:
Enter positive integer value for n and r
3
2

nPr Factorial of 3 and 2 is 6.00

Output 2:
Enter positive integer value for n and r
6
2

nPr Factorial of 6 and 2 is 30.00

Source Code: C Program To Find nPr Factorial using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float npr;

    printf("Enter positive integer value for n and r\n");
    scanf("%d%d", &n, &r);

    npr = factorial(n) / factorial(n-r);

    printf("\nnPr Factorial of %d and %d is %0.2f\n", n, r, npr);

    return 0;
}

int factorial(int num)
{
  return( (num != 0) ? (num * factorial(num - 1)) : 1);
}

Output 1:
Enter positive integer value for n and r
3
2

nPr Factorial of 3 and 2 is 6.00

Output 2:
Enter positive integer value for n and r
6
2

nPr Factorial of 6 and 2 is 30.00

Logic To Find nPr Factorial of n and r

We ask the user to input positive integer value for n and r. We then make use of nPr formula to get the nPr value.

To find Factorial logic, please visit the links present below in “Related Read” section below:

Related Read:
C Program To Find Factorial of a Number using Function
C Program To Find Factorial of a Number using Recursion

We have separate short videos explaining the iterative logic, recursive logic to find factorial of a user input number.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find nCr Factorial

Lets write a C Program to Find nCr Factorial for the user input values of n and r.

In this video tutorial we are showing iterative logic, recursive logic and how to write single line of code for getting factorial using recursion and ternary/conditional operator.

Formula To Calculate nCr Factorial

nCr = n! / (r! * (n-r)!);

Definition:

The number of different, unordered combination of r objects from a set of n objects is called nCr Factorial of n and r.

Very Important Note: n value should always be greater than value of r. Because, for example, we can get 2 out of 6. But we can’t get 6 out of 2. i.e., the main set should always be greater than the subset.

Example:

Lets assume that there are 3 people in a park. Let their names be A, B and C. There are only 2 seats available to sit. So the possible ways for people to sit over the seat using combination are {AB, AC, BC}. Since nCr is unordered combination AB and BA are considered same, so we use only one of them in our set. So in our example, we have 3 people, so n = 3. And we have 2 seats available, so r = 2. Finally we get 3 objects/combination in a set, so nCr is 3, when n = 3 and r = 2.

Factorial Definition: Factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Important Note: By convention, Factorial of 0 is 1. i.e., 0! = 1.

Example: 5! = 5 x 4 x 3 x 2 x 1 which is equal to 120. i.e., 5! = 120.

Video Tutorial: C Program To Find nCr Factorial


[youtube https://www.youtube.com/watch?v=s9fF8aSfQwg]

YouTube Link: https://www.youtube.com/watch?v=s9fF8aSfQwg [Watch the Video In Full Screen.]

Source Code: C Program To Find nCr Factorial

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float ncr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    ncr = factorial(n) / ( factorial(r) * factorial(n - r) );

    printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr);

    return 0;
}

int factorial(int num)
{
    int fact = 1, count;

    for(count = 1; count <= num; count++)
    {
        fact = fact * count;
    }
    return(fact);
}

Output 1:
Enter a positive value for n and r
3
2

nCr Factor of 3 and 2 is 3.00.

Output 2:
Enter a positive value for n and r
6
2

nCr Factor of 6 and 2 is 15.00.

Source Code: C Program To Find nCr Factorial using Recursion

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float ncr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    ncr = factorial(n) / ( factorial(r) * factorial(n - r) );

    printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr);

    return 0;
}

int factorial(int num)
{
    if(num)
        return(num * factorial(num-1));
    else
        return 1;
}

Output 1:
Enter a positive value for n and r
3
2

nCr Factor of 3 and 2 is 3.00.

Output 2:
Enter a positive value for n and r
6
2

nCr Factor of 6 and 2 is 15.00.

Source Code: C Program To Find nCr Factorial using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float ncr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    ncr = factorial(n) / ( factorial(r) * factorial(n - r) );

    printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr);

    return 0;
}

int factorial(int num)
{
  return( (num != 0) ? num * factorial(num-1) : 1);
}

Output 1:
Enter a positive value for n and r
3
2

nCr Factor of 3 and 2 is 3.00.

Output 2:
Enter a positive value for n and r
6
2

nCr Factor of 6 and 2 is 15.00.

Logic To Find nCr Factorial of n and r

We ask the user to input positive integer value for n and r. We then make use of nCr formula to get the nCr value.

To find Factorial logic, please visit the links present below in “Related Read” section.

Related Read:
C Program To Find Factorial of a Number using Function
C Program To Find Factorial of a Number using Recursion

We have separate short videos explaining the iterative logic, recursive logic to find factorial of a user input number.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert