beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

C program to calculate and print the value of nCr

By Chaitanya Singh | Filed Under: C Programs

In the following program we are calculating and displaying the value of nCr. nCr can also be represented as C(n,r)
The formula is:
C(n,r) = n! / ( r!(n – r)! ). For 0 <= r <= n. Here ! represents factorial. For example: C(6, 2) = 6! / (2! * (6-2)!) => 720/(2 * 24) => 15
The same calculation we have done in the following program.

#include <stdio.h>
 
int fact(int num);
 
void main()
{
    int n, r, ncr_var;
 
    printf("Enter the value of n:");
    scanf("%d", &n);
    printf("\nEnter the value of r:");
    scanf("%d", &r);
    /* ncr is also represented as C(n,r), the formula is:
     * C(n,r) = n! / ( r!(n - r)! ). For 0 <= r <= n.
     */
    ncr_var = fact(n) / (fact(r) * fact(n - r));
    printf("\nThe value of C(%d,%d) is: %d",n,r,ncr_var);
}
/* This function is used to find the 
 * factorial of given number num
 */
int fact(int num)
{
    int k = 1, i;
    // factorial of 0 is 1
    if (num == 0)
    {
        return(k);
    }
    else
    {
        for (i = 1; i <= num; i++)
    {
            k = k * i;
	}
    }
    return(k);
}

Output:

Enter the value of n:
5 
Enter the value of r:
2
The value of C(6,2) is: 15

Enjoyed this post? Try these related posts

  1. Insertion Sort Program in C
  2. C Program to Find ASCII value of a Character
  3. C Program to check whether a Character is an Alphabet or not
  4. C Program to Find the Number of Elements in an Array
  5. Hello World Program in C
  6. C Program to check Leap Year

Leave a Reply Cancel reply

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

Programs

  • C Programs
  • Java Programs

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap