C program to print pascal triangle
In this post, you will learn different ways to print Pascal triangle pattern using C programming language. The pascal triangle program is generally asked in C coding tests and interviews.
Pascal triangle is a number pattern that reflects a triangle. Firstly, 1 is set at the top, and afterward, we begin placing the numbers in a three-sided design. The numbers that we get in each progression are the addition of the over two numbers. It is like the concept of triangular numbers. Each new number lies between two numbers and below them, and its value is the sum of the two numbers above it.
Algorithm to Print Pascal's Triangle in C
- Get an input value from the user.
- Using a for loop that ranges from 0 to n-1, append the sub-lists into the list.
- Then append 1 to the sub-lists.
- Then use a for loop to determine the value of the number inside the triangle.
- Print the Pascal's triangle according to the format.
- Exit
C program to print Pascal's Triangle without using Array
Here, we have used another method by using the binomial coefficient. In the Binomial Coefficient C(line, i), all the line starts with 1 and the ith entry in a row number line. The formula is given below-
C(line, i) = C(line, i-1) * (line - i + 1) / i
Here is the C program to print the Pascal's triangle using the binomial coefficient.
#include<stdio.h>
int main()
{
int n;
printf("Please enter the number of rows: ");
scanf("%d",&n);
for(int row=1; row<=n; row++)
{
int x=1;
for(int i=1; i<=row; i++)
{
printf("%d ",x);
x = x * (row-i)/i;
}
printf("\n");
}
return 0;
}
Output of the above code:
Please enter the number of rows: 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
C program to print Pascal's Triangle using Array
In the given C program, we use the nested for loop to print the Pascal triangle pattern. First, we ask the user to enter the number of rows that the Pascal triangle has. We use a for loop which ranges from 0 to a number of rows. Inside the loop, append sub-lists into an empty list. Then append 1 to all the sub-lists. The for loop determines the value of the number inside the triangle, which is the sum of the two numbers above it. The other for loop prints the Pascal's triangle.
#include<stdio.h>
int main(void)
{
int n, i, j ;
printf("Enter the number of rows: ");
scanf("%d",&n);
int x[n][n];
for(i=0;i<n;++i)
{
for(int y=1; y<=40-i; y++)
printf(" "); //space
for(int j=0;j<=i;++j)
{
if(j==0||j==i)
x[i][j]=1;
else
x[i][j] = x[i-1][j-1] + x[i-1][j];
printf("%d ",x[i][j]);
}
printf("\n");
}
return 0 ;
}
Output of the above code:
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
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative