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

  • SEARCH
  • Home
  • 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 8928383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:25:34+00:00 2026-06-15T08:25:34+00:00

Note : My Class Teacher gave me this question as an assignment… I am

  • 0

Note : My Class Teacher gave me this question as an assignment… I am not asked to do it but please tell me how to do it with recursion

Binomial coefficients can be calculated using Pascal’s
triangle:

            1                   n = 0
         1     1
      1     2     1
   1     3     3     1
1     4     6     4     1       n = 4

Each new level of the triangle has 1’s on the ends; the interior numbers are the sums of the two numbers above them.

Task: Write a program that includes a recursive function to produce a list of binomial coefficients for the power n using the Pascal’s triangle technique. For example,

Input = 2 Output = 1 2 1

Input = 4 Output = 1 4 6 4 1

done this So Far but tell me how to do this with recursion…

#include<stdio.h>

int main()    
{
  int length,i,j,k;
//Accepting length from user
  printf("Enter the length of pascal's triangle : ");
  scanf("%d",&length);
//Printing the pascal's triangle
  for(i=1;i<=length;i++)
  {
      for(j=1;j<=length-i;j++)      
      printf(" ");
      for(k=1;k<i;k++)
          printf("%d",k);
      for(k=i;k>=1;k--)
          printf("%d",k);
      printf("\n");
  }
  return 0;    
}
  • 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-06-15T08:25:36+00:00Added an answer on June 15, 2026 at 8:25 am

    Method 1:

    Simplest way is to use the binomial coefficients. With this method, you have 1 recursive method:

    // Here is your recursive function!
    // Ok ok, that's cheating...
    unsigned int fact(unsigned int n)
    {
        if(n == 0) return 1;
        else return n * fact(n - 1);
    }
    
    unsigned int binom(unsigned int n, unsigned k)
    {
        // Not very optimized (useless multiplications)
        // But that's not really a problem: the number will overflow
        // way earlier than you will notice any performance problem...
        return fact(n) / (fact(k) * fact(n - k));
    }
    
    std::vector<unsigned int> pascal(unsigned n)
    {
        std::vector<unsigned int> res;
        for(unsigned int k = 0; k <= n; k++)
            res.push_back(binom(n,k));
        return res;
    }
    

    Live example.

    Method 2:

    This method use the construction with the formulae:

    Binomial formulae for Pascal's triangle

    Here, the only function is recursive and compute one line at a time, storing these result in an array (in order to cache results).

    std::vector<unsigned int> pascal(unsigned int n)
    {
        // This variable is static, to cache results.
        // Not a problem, as long as mathematics do not change between two calls,
        // which is unlikely to happen, hopefully.
        static std::vector<std::vector<unsigned int> > triangle;
    
        if(triangle.size() <= n)
        {
            // Compute for i = last to n-1
            while(triangle.size() != n)
                pascal(triangle.size());
    
            // Compute for n
            if(n == 0)
                triangle.push_back(std::vector<unsigned int>(1,1));
            else
            {
                std::vector<unsigned int> result(n + 1, 0);
                for(unsigned int k = 0; k <= n; k++)
                {
                    unsigned int l = (k > 0 ? triangle[n - 1][k - 1] : 0);
                    unsigned int r = (k < n ? triangle[n - 1][k] : 0);
                    result[k] = l + r;
                }
                triangle.push_back(result);
            }
        }
    
        // Finish
        return triangle[n];
    }
    

    Live example.

    Others methods:

    There are some other exotic methods, using the properties of the triangle. You can also use the matrix way to generate it:

    Pascal triangle from exponential of matrix

    No code for it, as it would require a lot of base code (matrices, exponential of matrix, etc…), and it is not really recursive.

    On a side note, I think this problem is absolutely not the right problem to teach recursion. There are a lot of better cases for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: Not sure if this is the right stack, please tell if I should
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
(Note: This is not a question about what is the best way with code
First: I know my question is not about a parent class, but I don't
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
NOTE: This is a followup to my question here. I have a program that
NOTE: This is an old question and the answers here no longer works (since
Note: I'm aware about the disadvantages of using BinaryFormatter in large files. But this
Okay, so there's another similar question that I found to this on SO but
I have created the class like below. But Override method is not called. Is

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.