BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C program to calculate and print the value of nCr

Last Updated: May 22, 2024 by Chaitanya Singh | Filed Under: C Programs

In this article, we will write a C program to calculate and print the value of nCr. The formula of nCr is: nCr = n! / ( r!(n - r)! ). For 0 <= r <= n. Here ! represents factorial. For example: 6C2 = 6! / (2! * (6-2)!) => 720/(2 * 24) => 15.

C Program to print the value of nCr based on user input

In this program, we have defined two functions. The factorial() function is to calculate the factorial, which is required in the nCr formula. The second function nCr() is to calculate the value of nCr, this would use the factorial() function.

#include <stdio.h>

/* This function calculates the factorial
 * of the passed number n and returns it
 */
unsigned long long factorial(int n) {
    unsigned long long fact = 1;

    // Loop to calculate the factorial of n
    for (int i = 1; i <= n; ++i) {
        fact *= i;
    }
    return fact;
}

// Function to calculate nCr
unsigned long long nCr(int n, int r) {
    // If r is greater than n, nCr is 0
    if (r > n) return 0;
 
    // Calculate nCr using the formula: nCr = n! / (r! * (n-r)!)
    return factorial(n) / (factorial(r) * factorial(n - r));
}

int main() {
    int n, r;
    
    // Prompt user to enter the values of n and r
    printf("Enter value of n: ");
    scanf("%d", &n); //store the entered value in n
    printf("Enter value of r: ");
    scanf("%d", &r); //store the entered value in r
    
    // Calculate nCr by calling the function
    unsigned long long result = nCr(n, r);
    
    // Print the value of nCr for entered values of n and r
    printf("%dC%d = %llu\n", n, r, result);
    
    return 0;
}

Output:

Output 1: when value of n is greater than r

Enter value of n: 5
Enter value of r: 2
5C2 = 10

Output 2: when value of r is 0

Enter value of n: 6
Enter value of r: 0
6C0 = 1

Output 3: when value of n and r are same

Enter value of n: 7
Enter value of r: 7
7C7 = 1

Output 4: when value of r is greater than n

Enter value of n: 8
Enter value of r: 9
8C9 = 0

Related C Programs:

  • C Program to find factorial of a number
  • C Program to find number of elements in an array
  • C Program to display factors of a number
  • C Program to swap first and last digits of a number
  • Java Program to find factorial of a number

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap