Write a c program to find out NCR factor of given number




Write a c program to find out NCR factor of given number Or
C program to find the ncr value by using recursive function

#include<stdio.h>
int main(){
  int n,r,ncr;
  printf("Enter any two numbers->");
  scanf("%d %d",&n,&r);
  ncr=fact(n)/(fact(r)*fact(n-r));
  printf("The NCR factor of %d and %d is %d",n,r,ncr);
  return 0;
}
 int fact(int n){
  int i=1;
  while(n!=0){
      i=i*n;
      n--;
  }
  return i;
 }


Algorithm:

In the mathematics nCr has defined as
nCr = n! /((n-r)!r!)





9 comments:

harshwardhan said...

nahi chalta hai

Anonymous said...

yas its running :).....u hv 2 define the function fact first

Anonymous said...

ritly said just define the function and it will work.,.. just apply your basic skills..u cannot get the rite code on net... n i think its bettter coz atleast u can act upon the program to correct it,thereby learning the program in a better weay...

Anonymous said...

#include
#include
viodmain(0
{
int n,r,i,f1,f2,f3,ncr;
clrscr();
printf("input n and r values");
scanf("%d%d",&n,&r);
f1=1;f2=1;f3=1;
for(i=1;i<=n;i++) f1*=i;
for(i=1;i<=r;i++) f2*=i;
for(i=1;i<=n-r;i++) f3*=i;
ncr=f1/(f2*f3);
printf("\n ncr=%d",ncr);
getch();
}

Labdhi Ghelani said...

#include
void main()
{
int n,r,i,f=1,a,b,j,g=1,c,k,h=1,d,factorial;
printf("enter the values of n and r\n");
scanf("%d%d",&n,&r);
for(i=1;i<=n;i++)
{
f=f*i;
a=f;
}
b=n-r;
for(j=1;j<=b;j++)
{
g=g*j;
c=g;
}
for(k=1;k<=r;k++)
{
h=h*k;
d=h;
}
factorial=(a/(c*d));
printf("the ncr are factorial of a given no is %d",factorial);
}

Madhu Rk said...

#include
#include
int ncr(int n,int r)
{
int ncr=1;
if(n==0!!n==r)
{
ncr=ncr*0;
}
else if(r==0)
{
ncr=ncr*n;
}
else
{
ncr=ncr*ncr(n-1,r-1);
}
return(ncr);
}
void main()
{
printf("%d",ncr(2,1));
}

Rajat Verma said...

Please Add Prototype of Fact function After The Headers.Because In Some Compilers it shows Error.

Rajat Verma said...

I think This is nice Way To Prevent the Copy Paste of codes during assignments of several Courses.and Dummes never find why this not working :D
I like it.

Rajat Verma said...

I think This is nice Way To Prevent the Copy Paste of codes during assignments of several Courses.and Dummies never find why this not working :D
I like it.