Hii Everyone... I used to be an active member of this community... but because of studies i wasnt able to keep in touch with you guys..
Am an amateur programmer,and i need your help... I have written a programme to find nCr[n combination r] nCr=n!/(n-r)! I want the compiler to show the value of NC2 for all values of n<=10 ..So I added a for loop with variable i... but the loop inst working properly...I need your help...here is the code

/* A program to find nCr[ n!/r!(n-r)! ]
*/
#include<iostream.h>
#include<conio.h>

long factorial(int);

int main()

{

clrscr();

long i,n,r,f,f1,f2,fact,j;

cout<<"Enter the values of n and r :\t ";

cin>>n>>r;

for(i=1;i<n;i++)

{

f=factorial(i);

f1=factorial(r);

f2=factorial(i-r);

fact=f/(f1*f2);

cout<<"Result"<<fact<<'\n';

long factorial(int i)

long f;

if(i==1)

f=1;

else

f=i*factorial(i-1);

return(f);

}

getch();

}

are you want something that allows you to define a function in another function?...
c / c++ forbid it. you should define long factorial(int); before main, or leave a declaration before main and make the implementation anywhere you like.

by the way, dont forget return in main.

You declared your factorial function in a for loop in main. French people would say : Il faut le faire!

/* A program to find nCr[ n!/r!(n-r)! ]
*/
#include<iostream.h>
#include<conio.h>

long factorial(int);

int main()

{

clrscr();

long i,n,r,f,f1,f2,fact,j;

cout<<"Enter the values of n and r :\t ";

cin>>n>>r;

for(i=1;i<n;i++)

{

f=factorial(i);

f1=factorial(r);

f2=factorial(i-r);

fact=f/(f1*f2);

cout<<"Result"<<fact<<'\n';

long factorial(long i)
{
   if(i==0)
     return 1;
   else
     return (i * factorial(i-1));
}
getch();

}

Here is the new code... still its not working... am using borland turbo c++ v3.1... pls help me

use code tags to format your code, I do it here:

long factorial(long i)
{
   if(i==0)
     return 1;
   else
     return (i * factorial(i-1));
}
getch();

is STILL in your for loop so why do you expect it should work?

Can't you read? Need my glasses?
PUT

long factorial(long i)
{
   if(i==0)
     return 1;
   else
     return (i * factorial(i-1));
}

OUTSIDE (if I could I would write it bigger) I mean OUTSIDE your for loop. A for loop is something that starts with { and ends with }
If you don't see this (this is the second time I tell you) stop programming, go fishing whatever...

sorry I dont understand you.. i have that code in my programme..! :)

Functions should be declared outside the main() function.
Example:

long int factorial(long int);

int main(void)
{
// blah blah blah
} // main() ends

/*now you can declare another function.
Can also be before main(), but not inside it */
long int factorial(long int i)
{
// now you can write the function
}

No u dont..

long factorial(long i)
{
   printf("%d, ",i);
   if(i==0)
     return 1;
   return (i * factorial(i-1));
}
getch();

is STILL in your for loop so why do you expect it should work?

plus..

int main()
{
   int i=factorial(5);
   return 0;
}

The function nCr should not be calculated using direct factorials. Very often nCr is within the MAXINT range BUT the factorials are not.
You should use the property of the factorials.

An initial optimization is to divide on the n/2 decision e.g.

long int nCr(const int n,const int r)
{
  long int result(1);
  long int dFactor(1);
  
  const int cnt((r<n/2) ? r : n-r);
  int s(n);
  for(int i=1;i<=cnt;i++)
    {
      result*=s--;
      dFactor*=i;
    }
  return result/dFactor;
}

There are a number of special cases, that should be programmed up (e.g. n/2==r). Additionally, 2,3, 5, and other prime factors can be removed from result and dFactor at intermediate points in the inner loop. With simple stuff like that you can keep nCr valid until n-r and n get very big, while int dies at 16, and long int at 20. Unsigned int gains you +1 on those numbers [Machine dependent!].

This article has been dead for over six months. Start a new discussion instead.