Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:23:23+00:00 2026-05-26T15:23:23+00:00

I have a small program. I have to calculate combination with repetition. My code:

  • 0

I have a small program. I have to calculate combination with repetition.
My code:

int factorial(int a){

    if (a<0)
    return 0;
    if (a==0 || a==1)
    return 1;
    return factorial(a-1)*a;
}
long int combinationWithRepetion(int n, int k){
    long int a,b,wyn=0;

    wyn=factorial(n+(k-1))/(factorial(k)*factorial(n-1));

    return wyn;
}
int main()
{
    int k,n=0;
    cout<<"Give n: ";
    cin>>n;
    cout<<"Give k: ";
    cin>>k;
    cout<<"combination With Repetion for n="<<n<<
    " k="<<k<<".\n Is equal to "<<combinationWithRepetion(n,k)<<endl;
    return 0;
}

For n=9 and k=6 in Wolfram alfa I get 3003, but in this program the result is 44.

For me the code is fine.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:23:24+00:00Added an answer on May 26, 2026 at 3:23 pm

    With n=9 and k=6 you compute factorial(14) which is 87,178,291,200 which will overflow a 4-byte int. You need to use something like long long to get an 8-byte int if you want to use this formula.

    There are better formulas for computing binomial coefficients which do not rely on computing the full factorials then doing the division. See Binomial coefficient in programming languages, the direct method (rather than using recursion).

    In C++ you can use:

    int binomial(int N, int K) {
      if( K > N - K )
        K = N - K;
      int c = 1;
      for(int i = 0; i < K; ++i) {
        c *= (N - i);
        c /= (i + 1);
      }
      return c;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small C program to calculate hashes (for hash tables). The code
I have this small C program (play.c): #include <stdio.h> main() { int t; for
As a Christmas gift I have written a small program in Java to calculate
I have a small program <350ish lines of code on 5 forms>. It works
I have a small program that mmaps potentially dangerous executable code (with PROT_EXEC), calls
I have a small test program. When I look into stack object for main
I have a small program to order and sort email messages, outputting to a
i have a small program where an element is draged and dropped, when the
I have a small program to do in Java. I have a 2D array
I have a small program I want to execute to test something #include <map>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.