C Program to Find nCr

This is a C Program to calculate the value of nCr.

Problem Description

This C Program Calculates the value of nCr.

Problem Solution

The algorithm used in this program is nCr = n! /((n-r)!r!). Here we need to find all the possible combination of the value n and r. A combination is one or more elements selected from a set without regard to the order. The “without regard” means that the collection matters rather than order in combinations, so in the above example, the fact we ABC, ACB, BAC, BCA, CAB, CBA… for combinations, these are all 1 combinationof letters A, B and C.

Program/Source Code

Here is source code of the C program to Calculate the value of nCr. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to Calculate the value of nCr
 */
#include <stdio.h>
 
int fact(int z);
 
void main()
{
    int n, r, ncr;
 
    printf("\n Enter the value for N and R \n");
    scanf("%d%d", &n, &r);
    ncr = fact(n) / (fact(r) * fact(n - r));
    printf("\n The value of ncr is: %d", ncr);
}
 
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);
}
Program Explanation

In this C Program, we are reading the value for ‘n’ and ‘r’ variable to calculate the value of nCr. The algorithm used in this program is nCr = n! /((n-r)!r!). A combination is one or more elements selected from a set without regard to the order.

advertisement
advertisement

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 ‘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 ‘z’ variable. If the condition is true then execute the loop. Multiply the value of ‘f’ variable with each integer variable value in ‘i’ variable. Compute the value for nCr and print the value of nCr using printf statement.

Runtime Test Cases
 
Output:
$ cc pgm12.c
$ a.out
Enter the value for N and R
5 2
 
The value of ncr is: 10

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.