C Program to Print nCr using While loop

C Program to Print nCr using While loop

Write C Program to Print nCr using While loop

//WAP to print nCr

#include <stdio.h>

int fact(n)
{
	int f = 1;
	int i = 1;
	while(i <= n)
	{
		f = f * i;
		i++;
	}
	return f;
}

int main()
{
	int n, r;
	printf("Enter the value of n:--> ");
	scanf("%d", &n);

	printf("Enter the value of r :--> ");
	scanf("%d", &r);

	int result = fact(n)/(fact(r)*fact(n-r));  //combination formula

	printf("The value of nCr is %d", result);

	return 0;
}

Output:

Enter the value of n:--> 5
Enter the value of r :--> 3

The value of nPr is 60