Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Program to print Pascal Triangle in C

by suresh

How to write a program to print Pascal Triangle in C language with example?. The C Pascal Triangle is a triangle with an array of binomial coefficients.

Program to print Pascal Triangle in C language

This pascal triangle in the C program allows the user to enter the maximum number of rows he/she want to print as a pascal triangle. We are going to print the pascal triangle of integers until it reaches the user-specified rows.

/* Program to print Pascal Triangle in C language */
#include <stdio.h>
 
long Factorial(int);
 
int main()
{
   int i, Number, j;
 
   printf("\n Please Enter the Number of rows you wish to see in pascal triangle\n");
   scanf("%d", &Number);
 
   for (i = 0; i < Number; i++)
   {
      for (j = 0; j <= (Number - i - 2); j++)
      {
      	printf(" ");
	  }
 
      for (j = 0; j <= i; j++)
      {
      	printf("%ld ", Factorial(i) / (Factorial(j) * Factorial(i-j)));
	  } 
 
      printf("\n");
   }
 
   return 0;
}
 
long Factorial(int Number)
{
   int i;
   long Fact = 1;
 
   for (i = 1; i <= Number; i++)
         Fact = Fact * i;
 
   return Fact;
}

OUTPUT

Program to print Pascal Triangle in C 1

ANALYSIS

Within this pascal triangle in c example, the following function declaration will find the factorial of the given number. Please refer C Program to Find Factorial of a Number to understand the steps involved in the first function

long Factorial(int Number)

The following C Programming statements will allow the User to enter the range or maximum Number of rows he/she want to print as a pascal triangle.

printf("\n Please Enter the Number of rows you wish to see in pascal triangle\n");
scanf("%d", &Number);

Now let us see the Nested for loop

for (i = 0; i < Number; i++)
{
   for (j = 0; j <= (Number - i - 2); j++) // First Inner Loop
   {
  	printf(" ");
   }
   for (j = 0; j <= i; j++) // Second Inner Loop
   {
     	printf("%ld ", Factorial(i) / (Factorial(j) * Factorial(i-j)));
   } 
   printf("\n");
}

Outer Loop – First Iteration

From the above screenshot, you can observe that the value of i is 0, the Number is 3, and the condition (i < 3) is True. So, it will enter into second for loop

First Inner Loop – First Iteration
The j value is 0 and the condition (j <= (3 – 0 – 2) is True. So, it will start executing the statements inside the loop. The following statement will print Empty space as Output

printf(" ");

Following statement in will increment the Value of j by 1 using the Increment Operator

j++

First Inner Loop – Second Iteration

The j value will be 1, and the condition (1 <= (3 – 0 – 2)) is True so that the following statement will print Empty space as Output

printf(" ");

First Inner Loop – Third Iteration

The j value will be two, and the condition (2 <= (3 – 0 – 2)) is False. So, it will exit from the for loop.

Next, it will enter into second For Loop

for (j = 0; j <= i; j++) // Second Inner Loop
   {
     	printf("%ld ", Factorial(i) / (Factorial(j) * Factorial(i-j)));
   }

Second Inner Loop – First Iteration
We haven’t finished the first iteration of the Outer for loop so, i value is still 0.

The j value is 0 and the condition (j <= 0) is True. So, it will start executing the statements inside the loop. The following statement will find the factorials of i and j and prints the output

printf("%ld ", Factorial(i) / (Factorial(j) * Factorial(i-j)));

factorial(i) / (Factorial(j) * Factorial(i – j) )
It means, factorial(0) / (Factorial(0) * Factorial(0) ) —> Factorial of 0 = 1
= 1 / (1 * 1) = 1

The following statement will increment the value of j by 1 using the Increment Operator

j++

Second Inner Loop – Second Iteration

The j value will be 1, and the condition (1 <= 0) is False. So, it will exit from the for loop. The following statement is to terminate the current line

printf("\n");

Final Output after the First Iteration = [Empty Space] [Empty Space] 1

Outer Loop – Second Iteration
The value of i will be ,1 and the condition (1 < 3) is True. So, it will enter into second for loop

First Inner Loop – First Iteration

The value of j is 0 and the condition (0 <= 0) is True. So, empty space will print as output. Next, the j value will also be incremented by 1.

First Inner Loop – Second Iteration

The value of j is 1, and the condition (1 <= 0) is False. So, it will exit from the for loop. Now, it will enter into second For Loop

Second Inner Loop – First Iteration
We haven’t finished the second iteration of the Outer for loop so, i value is still 1.

The j value is 0 and the condition (0 <= 1) is True. So, it will start finding the factorials of i and j and prints the output

factorial(i) / (Factorial(j) * Factorial(i – j) )
It means, factorial(1) / (Factorial(0) * Factorial(1) ) —> Factorial of 0 = 1
= 1 / (1 * 1) = 1

Second Inner Loop – Second Iteration
The j value will be 1 and the condition (1 <= 1) is True So, it will start finding the factorials of i and j and prints the output

factorial(i) / (Factorial(j) * Factorial(i – j) )
It means, factorial(1) / (Factorial(1) * Factorial(0) ) —> Factorial of 0 = 1
= 1 / (1 * 1) = 1

Second Inner Loop – Third Iteration

The j value will be 2, and the condition (2 <= 1) is False so that it will exit from the for loop. Final Output after the Second Iteration = [Empty Space] 1 1

Outer Loop – Third Iteration
The value of i will be 2, and the condition (2 < 3) is True. So it will enter into second For Loop

Second Inner Loop – First Iteration
We haven’t finished the third iteration of the Outer for loop so, i value is still 2.

The j value is 0 and the condition (0 <= 2) is True. So, it will start finding the factorials of i and j and prints the output

factorial(i) / (Factorial(j) * Factorial(i – j) )
It means, factorial(2) / (Factorial(0) * Factorial(2) ) —> Factorial of 0 = 1
= 2 / (1 * 2) = 1

Second Inner Loop – Second Iteration
The j value will be 1, and the condition (1 <= 2) is True. So, it will start finding the factorials of i and j and prints the output

factorial(i) / (Factorial(j) * Factorial(i – j) )
It means, factorial(2) / (Factorial(1) * Factorial(1) ) —> Factorial of 2 = 2
= 2 / (1 * 1) = 2

Second Inner Loop – Third Iteration
j = 2, and the condition (2 <= 2) is True. So, it will start finding the factorials of i and j and prints the output

factorial(i) / (Factorial(j) * Factorial(i – j) )
It means, factorial(2) / (Factorial(2) * Factorial(0) ) —> Factorial of 2 = 2
= 2 / (2 * 1) = 1

Second Inner Loop – Fourth Iteration
j = 3, and the condition (3 <= 2) is False. So, it will exit from the for loop. The final Output after the Third Iteration = 1 2 1

Outer Loop – Fourth Iteration

The value of i will be 3, and the condition (3 < 3) is False. So it will exit from the for loop.

Final Output is:

[Empty Space] [Empty Space] 1
= [Empty Space] 1 1
1 2 1

C Program to print Pascal Triangle in C using recursion

This C program for the pascal triangle in c allows the user to enter the number of rows he/she want to print as a Pascal triangle. In this example, we are going to use the code snippet that we used in our first example. However, this time we are using the recursive function to find factorial.

Please refer C Program to Find Factorial of a Number to understand the steps involved in the first function, i.e., long factorial (int Number)

/* Program to print Pascal Triangle in C using recursion */
#include <stdio.h>
long Factorial(int Number)
{
  if (Number == 0 || Number == 1)  
    return 1;
  else
    return Number * Factorial (Number -1);
}

int main()
{
   int i, Number, j, Fact;
   printf("\n Please Enter Number of rows you want to see in pascal triangle\n");
   scanf("%d", &Number);
   for (i = 0; i < Number; i++)
   {
      for (j = 0; j <= (Number - i - 2); j++)
      {
      	printf(" ");
      }
      for (j = 0; j <= i; j++)
      {
      	Fact = Factorial(i) / (Factorial(j) * Factorial(i-j));
	printf("%ld ", Fact);
      } 
      printf("\n");
   }
   return 0;
}

OUTPUT

Program to print Pascal Triangle in C 2

Placed Under: C Programs

  • C Hello World
  • C Add Two numbers
  • C Armstrong Number Program
  • C Convert Celsius to Fahrenheit
  • C Convert Decimal to Binary
  • C Convert Decimal to Octal
  • C Convert CM to Meter and KM
  • C Convert KM to M, CM & MM
  • C Convert Fahrenheit to Celsius
  • C Compound Interest Program
  • C Count Digits in a Number
  • C Count Notes in an Amount
  • C Cube of a Number
  • C Electricity Bill Program
  • C Factors of a Number Program
  • C Factorial of a Number Program
  • C GCD of Two Numbers
  • C Generic Root of a Number
  • C Gross Salary of an Employee
  • C Largest of Two Numbers
  • C Largest of Three numbers
  • C Last Digit Of a Number
  • C LCM of Two Numbers
  • C Leap Year program
  • C Multiplication Table program
  • C Natural Numbers from 1 to N
  • C Natural Numbers in reverse
  • C NCR Factorial of a Number
  • C Number is Divisible by 5 and 11
  • C Palindrome Program
  • C Palindrome String program
  • C Perfect Number Program
  • C Profit or Loss Program
  • C Print 1 to 100
  • C Print Prime Numbers 1 to 100
  • C Prime Number program
  • C Prime Factors of a Number
  • C Prime, Armstrong or Perfect
  • C Positive or Negative program
  • C Print Odd Numbers 1 to N
  • C Print Even Numbers 1 to N
  • C Print Integer, Char & Float
  • C Power of a Number program
  • C Product of Digits in a Number
  • C Roots of a Quadratic Equation
  • C Reverse a Number
  • C Simple Calculator Program
  • C Simple Interest
  • C Square of a Number
  • C Square Root of a Number
  • C Standard Deviation program
  • C Strong Number Program
  • C Student Grade program
  • C Sum of Odd Numbers program
  • C Sum of Even Nums program
  • C Sum of Even and Odd Nums
  • C Swap First & Last digit of Num
  • C Sum of First & Last digit Num
  • C Sum of Digits of a Number
  • C Sum of N Numbers Program
  • C Sum and Average of n Number
  • C Swap 2 Numbers
  • C Total, Avg & % of Five Subjects
  • C Quick Sort
  • C Selection Sort
  • C Insertion Sort
  • C Bubble Sort
  • C Fibonacci Series Program
  • C Nth Fibonacci number
  • C Sum of AP Series
  • C Sum of GP Series
  • C Sum of series 1³+2³+3³+….+n³
  • C Sum of series 1²+2²+3²+….+n²
  • C ASCII value of String chars
  • C Print Characters in a String
  • C Compare Two Strings
  • C Concatenate Two Strings
  • C Copy String
  • C String length program
  • C Convert String to Lowercase
  • C First Occurrence of string char
  • C First Occur of String Word
  • C Count Occur of string Char
  • C Count Alphabets, Digits & Special Chars
  • C Count Vowels & Consonants
  • C string remove all Occ of char
  • C remove 1st Occ of char in string
  • C remove last Occ char in string
  • C replace All Occ of char in string
  • C Replace last Occ of String Char
  • C Replace 1st occ char in string
  • C Reverse words Order in string
  • C Reverse a String
  • C Toggle Case of all char in string
  • C Array Arithmetic Operations
  • C Matrix Arithmetic Operations
  • C Count Frequency of array item
  • C Count Duplicate Array items
  • C Count +ve & -Ve Array items
  • C Count Even & Odd Array items
  • C Copy an Array to another
  • C Delete Duplicate Array items
  • C Delete an Element in an Array
  • C insert an Element in an Array
  • C 2nd largest Number in an Array
  • C Find Largest Array Number
  • C Find Smallest Array Number
  • C Largest & Smallest Array item
  • C Merge Two Arrays
  • C Print Unique Array Elements
  • C Print Arrayy Elements
  • C Print Negative Array Numbers
  • C Print Positive Array Numbers
  • C Put +ve & -Ve in 2 Arrays
  • C Put Even & Odd in 2 Arrays
  • C Program to Reverse an Array
  • C Search an Element in an Array
  • C Sum of array even, odd nums
  • C Sort Array in Ascending Order
  • C Sort Array in Descending
  • C Swap 2 Arrays without Temp
  • C Sum of all Array Elements
  • C Sum of each Matrix column
  • C sum of each row in a Matrix
  • C Sum of Matrix row & column
  • C Add Two Matrices
  • C Sparse Matrix
  • C Symmetric Matrix
  • C Identity Matrix
  • C Interchange Matrix Diagonals
  • C Check two Matrices Equal
  • C Lower Triangle of a Matrix
  • C Upper Triangle of a Matrix
  • C Sum of Lower Triangle Matrix
  • C Sum of Upper Triangle Matrix
  • C Subtract Two Matrices
  • C Transpose of a Matrix program
  • C Cuboid Volume & Surface Area
  • C Cone Volume & Surface Area
  • C Cube Volume & Surface Area
  • C Volume & Surface of Cylinder
  • C Volume & Surface of Sphere
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy