ncr factor of a number

Find ncr factor of number

C program to find ncr factor of a number

We used the algorithm in this program is nCr = n! /((n-r)!r!). Here we need to find all the possible combinations of the value n and r.
This program allows the user to enter two positive integers. And then, it will calculate the NCR Factorial of the given number.

#include <stdio.h>
#include<conio.h>
int fact(int z);
void main()
{
    int n, r, ncr;
printf("\n\t\t\t...Welcome To EduNews.Tech... ");

    printf("\n\n Enter the value for N and R :");
    scanf("%d%d", &n, &r);
    ncr = fact(n) / (fact(r) * fact(n - r));
    printf("\n The value of ncr is : %d", ncr);
printf("\n\n\n\t\t\tThankyou for Joining Us !");
    printf("\n\t\t\t\t!Regards EduNews !");

}
int fact(int z)
{
    int f = 1, i;
    if (z == 0)
    {
        return(f);
    }
    else
    {
        for (i = 1; i <= z; i++)
    {
            f = f * i;
    }
    }
    return(f);

getch();
}

Program Output:

ncr factor

Explanation

Then ‘ncr’ variable is used to compute fact(n)/(fact(r)* fact(n – r)). The fact() function is used to compute the factorial of the value. If-else condition statement is used to check the argument value of the ‘z’ variable is equal to 0. If the condition is true then execute the statement.

For loop is used to compute the factorial value. Initialize the value of ‘i’ variable to 1 and check the value of ‘i’ variable is less than or equal to the argument value in the ‘z’ variable. If the condition is true then execute the loop. Multiply the value of ‘f’ variable with each integer variable value in the ‘i’ variable. Compute the value for nCr and print the value of nCr using printf statement.

I hope this post helps you to understand the “nCr factor of a number” and its implementation in C programming language.

Keep coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *