Pascal Triangle Program in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Pascal triangle is a triangular array of binomial coefficients. In pascal’s triangle, each number is the sum of the two numbers directly above it. Here we will write a pascal triangle program in the C programming language.

                                       1 
                                      1 1 
                                     1 2 1 
                                    1 3 3 1 
                                   1 4 6 4 1 

Pascal Triangle Program in C Without Using Array

#include<stdio.h>
int main()
{
  int n;
  printf("Enter the number of rows: ");
  scanf("%d",&n);
  for(int row=1; row<=n; row++)
  {
    int a=1;
    for(int i=1; i<=row; i++)
    {
      printf("%d ",a);
      a = a * (row-i)/i;
    }
    printf("\n");
  }
  return 0;
}

Output for different test-cases:-

Enter the number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Enter the number of rows: 7
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

At the center of the screen

We can display the pascal triangle at the center of the screen. For this, just add the spaces before displaying every row. Generally, on a computer screen, we can display a maximum of 80 characters horizontally. Half of 80 is 40, so 40th place is the center of the line.

#include<stdio.h>
int main()
{
  int n;
  printf("Enter the number of rows: ");
  scanf("%d",&n);
  for(int row=1; row<=n; row++)
  {
    int a=1;

    for(int s=1; s<=40-row; s++)
    printf(" ");

    for(int i=1; i<=row; i++)
    {
      printf("%d ",a);
      a = a * (row-i)/i;
    }

    printf("\n");
  }

  return 0;
}

Output for different test-cases:-

Enter the number of rows: 5
                                       1 
                                      1 1 
                                     1 2 1 
                                    1 3 3 1 
                                   1 4 6 4 1 
Enter the number of rows: 7
                                       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 

Pascal Triangle in C using 2d Array

#include<stdio.h>
int main(void)
{
  int n, i, j ;

  printf("Enter the number of rows: ");
  scanf("%d",&n);

  int pascal[n][n];
  for(i=0;i<n;++i)
  {
       
    for(int s=1; s<=40-i; s++)
    printf(" "); //space

    for(int j=0;j<=i;++j)
    {
      if(j==0||j==i) pascal[i][j]=1;
      else
      pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];

      printf("%d ",pascal[i][j]);
    }

    printf("\n");
  }

  return 0 ;
}

In this program, we took a 2d array of size n, where n is the number of rows entered from the user.

The first inner loop displays the space on the output screen. if you don’t want to display the output at the center of the screen then remove this for a loop.

In the pascal triangle, in every row, the first and last number is 1 and the remaining are the sum of the two numbers directly above it. So, inside the second inner for loop, we write the below condition,

if(j==0||j==i) pascal[i][j]=1;
else pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];

Note that in every row the size of the array is n, but in 1st row, the only first element is filled and the remaining have garbage value. Similarly, in the second row, only the first and second elements of the array are filled and remaining to have garbage value. We don’t want to display the garbage value. So, it will be easy for us to display the output at the time of calculation.

Learn more:- The mathematical secrets of Pascal’s triangle

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Comment

Your email address will not be published. Required fields are marked *