C program to print pascal triangle

devquora
devquora

Posted On: Apr 14, 2023

C program to print pascal triangle

 

Write a C Program to print Pascal triangle:

Overview:

First of all, in this article, I will give you a general introduction of the Pascal Triangle. Then, I will write c program to print the Pascal triangle. After all, I will show its output as a Pascal triangle in front of you.

Table of contents:

  1. What is Pascal’s Triangle?
  2. Demonstration of Pascal’s Triangle
  3. C program to print pascal triangle
  4. Conclusion

What is Pascal’s Triangle?

Pascal’s triangle, in algebra, a triangular arrangement of numbers that gives the coefficients in the expansion of any binomial expression, such as (x + y)n. It is named for the 17th-century French mathematician Blaise Pascal. 

To find nth term of a Pascal triangle we use the following formula.

 

Demo of Pascal Triangle

Demonstration of Pascal Triangle

Demo of Pascal Triangle

C program to print pascal triangle

#include <stdio.h>

/* Function definition */
long long fact(int n);

int main()
{
    int n, k, row, i;
    long long term;

    /* Input number of rows */
    printf("Enter number of rows : ");
    scanf("%d", &row);

    for(n=0; n<row; n++)
    {
        /* Prints 3 spaces */
        for(i=n; i<=row; i++)
            printf("%3c", ' ');

        /* Generate term for current row */
        for(k=0; k<=n; k++)
        {
            term = fact(n) / (fact(k) * fact(n-k));

            printf("%6lld", term);
        }

        printf("\n");
    }

    return 0;
}

/**Function to calculate factorial */
long long fact(int n)
{
    long long factorial = 1ll;
    while(n>=1)
    {
        factorial *= n;
        n--;
    }

    return factorial;
}

Demo of Pascal Triangle

Conclusion:

Pascal Triangle is named as the Mathematician Blaise Pascal. This Program shows a Pascal Triangle in front of you as a output.

    Please Login or Register to leave a response.

    Related Articles