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();

}

Dani AI

Generated

The immediate problem is that the factorial function is being defined inside main (inside the for block). C and C++ do not allow nested function definitions; as , and pointed out, the function must be declared/defined at file scope (either put the full definition before main or put a prototype before main and the definition after it). With a nested definition most compilers will emit a syntax error such as "nested functions are not allowed" or "expected declaration before 'long'".

There are two other logic bugs that will cause runtime trouble even after you fix the placement. First, your loop calls factorial(i - r) for values of i smaller than r; that passes a negative value into the factorial routine and leads to infinite recursion/undefined behaviour. Either start the loop at i = r (or test i >= r before computing) or guard the factorial function against invalid inputs. Second, be sure the factorial base case covers 0 (factorial(0) == 1), as reminded you.

A couple of practical suggestions: validate inputs (non‑negative, r <= n), return from main (e.g. return 0;), and avoid Borland-only cruft if portability matters — conio.h, clrscr() and getch() are nonstandard. For small n (<= 10) a 32‑bit long will hold factorials, but for larger values use 64‑bit (long long) or use a multiplicative/combinatorial algorithm (see ’s advice) to avoid overflow and compute nCr without computing full factorials.

Quick checklist to get a working program:

  • Move factorial out of main (or add a prototype and define it after main).
  • Ensure factorial(0) == 1 and reject negative inputs.
  • Loop from i = r to the desired upper bound (or check i >= r before calling factorial(i-r)).
  • Validate n and r and consider using an iterative or multiplicative nCr method for larger values.

Following these steps will resolve the compile error and the incorrect loop behaviour.

Recommended Answers

All 10 Replies

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.

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

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?

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

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!].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.