Computer Science‎ > ‎

C Program: Printing the Pascal Triangle


C Program to print the Pascal Triangle


/*This program prints the Pascal's triangle
The number of terms to be displayed is given as input by the user.
At the tip of Pascal's Triangle is the number 1, which makes up the zeroth row.
The first row contains two 1's, at the end both .
Do the same to create the 2nd row,make the elements at the ends as 1 and by adding the two numbers above and to the left and the right 1+1=2.
And the third: 1+2=3; 2+1=3.
In this way, the rows of the triangle go on infinitly.
A number in the triangle can also be found by nCr (n Choose r) where n is the number of the row and r is the element in that row.
*/
#include<stdio.h>
int main()
{
    int **tri;//A 2-D array is used to store the triangle
    int terms,ictr,octr;
    printf("Enter the number of terms you want to see in the triangle\n");
    scanf("%d",&terms);
    printf("\nPASCAL TRIANGLE\n");
    tri=(int **)malloc(terms*sizeof(int));
    for(ictr=0;ictr<terms;ictr++)
    {
        tri[ictr]=(int *)malloc((ictr+1)*sizeof(int));
        for(octr=0;octr<ictr+1;octr++)
        if(octr==0||octr==ictr)
            tri[ictr][octr]=1;//making elements at the ends as 1
        else
            tri[ictr][octr]=tri[ictr-1][octr-1]+tri[ictr-1][octr];//for any other element add the elemnt of previous row
    }
    for(ictr=0;ictr<terms;ictr++)
    {//display the triangle
        for(octr=0;octr<ictr+1;octr++)
        printf("%d ",tri[ictr][octr]);
        printf("\n");
        free(tri[ictr]);
    }
    free(tri);
    getch();
}
/*A sample run of the program was carried out and the results were found as:-
Enter the number of terms you want to see in the triangle
7
PASCAL TRIANGLE
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
*/


C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers