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

C Program To Find Factorial of a Number using Recursion

Lets write a C program to find Factorial of a user input number using Recursion.

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.

Formula To Calculate Factorial of any positive integer number
We can calculate factorial of any number using this relationship:

num! = num * (num – 1)!

where num is a positive integer number.

Related Read:
C Program To Find Factorial of a Number
Recursive Functions In C Programming Language

Video Tutorial: C Program To Find Factorial of a Number using Recursion


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

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


Source Code: C Program To Find Factorial of a Number using Recursion

#include<stdio.h>

int fact(int);

int main()
{
    int num;

    printf("Enter a positive number to find its Factorial\n");
    scanf("%d", &num);

    printf("\nFactorial of %d is %d.\n", num, fact(num));

    return 0;
}

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

Output 1:
Enter a positive number to find its Factorial
7

Factorial of 7 is 5040.

Output 2:
Enter a positive number to find its Factorial
8

Factorial of 8 is 40320.

Logic To Find Factorial of a Number using Recursion

We ask the user to enter a positive integer number and we pass this number to a function called fact().

Inside fact() function
Inside fact() function, we check if the number is non-zero, if true, we execute the code inside if block orelse(if num is zero), then the code inside else block gets executed.

Inside if block, we add the factorial of (num-1) to the value of num.

num * fact(num – 1)

and return the result to the calling function.

Inside else block, we simply return 1. Because factorial of 0 is 1. So when num is 0, we return 1.

Example:

numnum * fact(num-1)
55 * fact(4)
44 * fact(3)
33 * fact(2)
22 * fact(1)
11 * fact(0)

Value Returning – Control Shifting back.

FunctionReturn Value Result
1 * fact(0)return 1;1 * 1 = 1
2 * fact(1)12 * 1 = 2
3 * fact(2)23 * 2 = 6
4 * fact(3)64 * 6 = 24
5 * fact(4)245 * 24 = 120

Finally 120 will be returned to main() method where we call the fact() method.

Source Code: C Program To Find Factorial of a Number using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int fact(int);

int main()
{
    int num;

    printf("Enter a positive number to find its Factorial\n");
    scanf("%d", &num);

    printf("\nFactorial of %d is %d.\n", num, fact(num));

    return 0;
}

int fact(int num)
{
    return( (num > 0) ? (num * fact(num - 1)) : 1 );
}

Output 1:
Enter a positive number to find its Factorial
5

Factorial of 5 is 120.

Output 2:
Enter a positive number to find its Factorial
6

Factorial of 6 is 720.

Know more about ternary operator or conditional operator, watch a separate video tutorial: Ternary Operator / Conditional Operator In C.

Memory Stack

Whenever there is a recursive function call, function instance and the memory associated with that function instance gets pushed into the stack. When any function instance returns a value, that function instance and memory associated with that function instance gets popped out or removed or gets deleted from the memory stack.

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 Evaluate sin(x) = x – (x^3)/3! + (x^5)/5! + (x^7)/7! + ..

Write a C function to evaluate the series

sin(x) = x – x3/3! + x5/5! – x7/7! …

up to 10 terms.

Analyze Above Problem Statement

Sine series formula

1. In above sine series, observe the first term, which is x. Which can be written as x1/1!.

i.e., x = x1/1!;

2. Check the exponent and the number used to divide the value of x in each series. 1 followed by 3 which is followed by 5 and so on. If you observe closely they’re all odd numbers. Adding 2 to 1 gives you 3. Adding 2 to 3 gives you 5. Adding 2 to 5 gives you 7 and so on ..

So the sine series becomes:

sin(x) = x1/1! – x3/3! + x5/5! – x7/7! + x9/9! – x11/11! + x13/13! – x15/15! + x17/17! – x19/19!

Now we have all 10 terms in the sine series.

3. Now observe the sign. First term in the series is positive. Second term is negative. Third term is positive. Again the forth term is negative. So we can conclude that, first term starts with positive and then it’s an alternative between negative and positive.

4. Next we need to find factorial of each number which divides x in each series. i.e., 1, 3, 5, 7, 9, 11, 13, 15, 17, 19. (10 odd numbers)

Related Read:
Basics of Pointers In C Programming Language
Function / Methods In C Programming Language

Very Important Note:

Any address preceded by a * (Indirection operator) will fetch the value present at that address.

Video Tutorial: C Program To Evaluate sin(x) = x – (x^3)/3! + (x^5)/5! + (x^7)/7! + ..


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

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


Source Code: C Program To Evaluate sin(x) = x – (x^3)/3! + (x^5)/5! + (x^7)/7! + ..

#include<stdio.h>
#include<math.h>

double factorial(int);
void   calc(float, float*);

int main()
{
    int   x;
    float radian, result = 0;

    printf("Enter value of x in degrees\n");
    scanf("%d", &x);

    radian = x * (3.14159 / 180.0); // Convert Degree To Radian

    calc(radian, &result);

    printf("Sin(%d) = %f\n", x, result);

    return 0;
}

void calc(float num, float *res)
{
    int count, n = 1, sign = 1;

    for(count = 1; (n <= 10); count += 2)
    {
       *res  +=  sign * ( pow(num, count) / factorial(count) );
        n    +=  1;
        sign *= -1;
    }
}

double factorial(int num)
{
    int    count;
    double sum = 1;

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

Output 1:
Enter value of x is degrees
0
Sin(0) = 0.000000

Output 2:
Enter value of x is degrees
30
Sin(30) = 0.500000

Output 3:
Enter value of x is degrees
45
Sin(45) = 0.707106

Output 4:
Enter value of x is degrees
60
Sin(60) = 0.866025

Output 5:
Enter value of x is degrees
90
Sin(90) = 1.000000

Check above output with the value present in below chart. If you further evaluate the values present in below chart, it matches with the output of our program:

Sine series values

Compound Assignment Operator

We are using compound assignment operator in this program.

 sum   *=  count;
 count +=  2
*res   +=  sign * ( pow(num, count) / factorial(count) );
 n     +=  1;
 sign  *= -1;

which is equal to writing:

 sum   =  sum * count;
 count =  count + 2;
*res   = *res + sign * ( pow(num, count) / factorial(count) );
 n     =  n + 1;
 sign  =  sign * -1;

Usually many advanced developers use such shorthand notations while writing the programs. So we need to at least understand their code while going through it on their git repository or a simple source file.

Compound Assignment Operators in C

Logic To Evaluate sin(x) = x – (x^3)/3! + (x^5)/5! + (x^7)/7! + ..

User enters value of x in degrees. We convert it to radian value using the formula:

radian = x * (3.14159 / 180.0);

C Program To Convert Degree To Radian

We pass the value of radian and address of another floating point variable result to a function calc();

Inside calc() function
Inside calc() function we iterate the for loop 10 times, as we need to find 10 terms in the series(according to the problem statement). We initialize the value of count(loop counter variable) to 1. For each iteration of for loop we increment the value of count by 2. For 10 iterations count value changes as follows: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19.

Inside for loop we calculate each series one by one:
*res += sign * ( pow(num, count) / factorial(count) );
sign *= -1;

We initialize the value of variable sign to 1. And then we alternate the sign from positive to negative, and negative to positive for each iteration.

We are making use of pow() builtin method to find num to the power of count. pow() is present in math.h library file, so we include it in the header of our program.

Calculate Power of a Number using pow(): C Program

We also call a method called factorial() to find factorial of the value present in variable count.

Inside factorial() function
Inside factorial() function we calculate the factorial of the passed number.

Factorial of a number is the product of all the numbers preceding it. For example, Factorial of 6 is 720 (1 x 2 x 3 x 4 x 5 x 6 = 720). Denoted by 6! = 720;

C Program To Find Factorial of a Number using Function

In general, n objects can be arranged in n(n – 1)(n – 2) … (3)(2)(1) ways. This product is represented by the symbol n!, which is called n factorial. By convention, 0! = 1.

Very Important Note

In factorial() function, variable sum has a return type of double. That is because int(integer) type variable can only hold up to 4 bytes of data. Where as double type variable can hold up to 8 bytes. Ofcourse these memory allocations are machine dependent, however on any machine double type variable holds more bytes than int type. So we prefer using double here as we’ll be calculating factorial for bigger numbers i.e., from 1 to 19. Since we’ll be returning value of sum from the factorial() function, we need to have a return type of double for the factorial() function too.

1.  1!  = 1.000000
2.  3!  = 6.000000
3.  5!  = 120.000000
4.  7!  = 5040.000000
5.  9!  = 362880.000000
6.  11! = 39916800.000000
7.  13! = 6227020800.000000
8.  15! = 1307674368000.000000
9.  17! = 355687428096000.000000
10. 19! = 121645100408832000.000000

Using sizeof() operator we can know the memory allocation for each data type: Sizeof Operator in C Programming Language.

Also note that the format specifier for double data type is %lf.

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 Factorial of a Number using Function

Write a function to calculate the factorial value of any integer entered through the keyboard.

Related Read:
C Program To Find Factorial of a Number

Factorial of a number is the product of all the numbers preceding it. For example, Factorial of 6 is 720 (1 x 2 x 3 x 4 x 5 x 6 = 720).

In general, n objects can be arranged in n(n – 1)(n – 2) … (3)(2)(1) ways. This product is represented by the symbol n!, which is called n factorial. By convention, 0! = 1.

Video Tutorial: C Program To Find Factorial of a Number using Function


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

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


Source Code: C Program To Find Factorial of a Number using Function

#include<stdio.h>

void factorial(int);

int main()
{
    int num;

    printf("Enter a positive number to find Factorial\n");
    scanf("%d", &num);

    factorial(num);

    return 0;
}

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

    if(num == 0)
    {
        printf("Factorial of 0 is 1 (!0 = 1)\n");
    }
    else
    {
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        printf("\nFactorial of %d is %d (!%d = %d)\n", num, fact, num, fact);
    }
}

Output 1:
Enter a positive number to find Factorial
5

Factorial of 5 is 120 (!5 = 120)

Output 2:
Enter a positive number to find Factorial
4

Factorial of 4 is 24 (!4 = 24)

Output 3:
Enter a positive number to find Factorial
6

Factorial of 6 is 720 (!6 = 720)

Output 4:
Enter a positive number to find Factorial
7

Factorial of 7 is 5040 (!7 = 5040)

Output 5:
Enter a positive number to find Factorial
8

Factorial of 8 is 40320 (!8 = 40320)

Logic To Find Factorial of a Number

Complete for loop logic to find Factorial of a number is present at C Program To Find Factorial of a Number. Watch the video without fail to understand the logic.

Note: Function factorial doesn’t return anything so its return type is void. It accepts 1 integer type argument.

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