Here's a C program to find the combination value (NCR) with output and explanation. The program contains a recursive function called fact() which calculates the factorial of a number and using this we're calculating the combination value.
# include <stdio.h> # include <conio.h> long fact(int) ; //function is declared at the top first. void main() { long i, ncr ; int n, r ; clrscr() ; printf("Enter the value for N : ") ; scanf("%d", &n) ; printf("\nEnter the value for R : ") ; scanf("%d", &r) ; if(n >= r) { ncr = fact(n) / (fact(n-r) * fact(r)) ; printf("\nThe NCR value is : %ld", ncr) ; } else printf("\nCalculating NCR value is not possible") ; getch() ; } //fact(int i) function definition after the end of main. //This function is a recursive function. //Recursive function are functions that call //themselves from within the function. long fact(int i) { long x ; if(i == 0) return 1 ; else { x = i * fact(i - 1) ; return x ; } }
Output of above program is
Enter the value for N : 7
Enter the value for R : 5
The NCR value is : 21
Hello There,
Your writing shines! There is no room for gibberish here clearly you have explained about
C Programming Tutorial Keep writing!
I'd really like to know what type of arguments does this function take, and what happens in the for loop.
I realize that first 2 are arrays, the next one is an unsigned number, what about the last two?
It was cool to see your article pop up in my Google search for the process yesterday. Great Guide.
Keep up the good work!
Grazie,
John