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!)





6 comments:

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

    ReplyDelete
  2. 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...

    ReplyDelete
  3. #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();
    }

    ReplyDelete
  4. #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);
    }

    ReplyDelete
  5. #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));
    }

    ReplyDelete