The Pascal triangle is one of the oldest and most interesting topics to learn in math. This is a well-known mathematical tool that is used to calculate many important values. In this tutorial, I will teach you how to write a C program to print the Pascal triangle.
Pascal triangle is nothing but a table that shows the numbers in ascending order. So, it is a kind of triangle that has three sides and each side is connected to the next one.
In this triangle, the numbers are arranged in an ascending order starting from 1, 2, and 3, …. This triangle is widely used in statistics to calculate the probability of various events.
Let us see some of its uses:
- To find the sum of all numbers in the triangle.
- To find the product of all numbers in the triangle.
- To find the factorial of all numbers in the triangle.
- To find the number of combinations of the given elements.
To calculate the sum of numbers in a triangle, you need to count the number of cells in the triangle.
For example, if the sum of the numbers in the triangle is 10, then you will have to count the total number of cells in the triangle.
Here is the code that will help you to find the sum of the numbers in the triangle.
Pascal’s triangle is a mathematical concept and also the name of a triangular array that shows the numbers 1, 2, 3, 4, etc. in each row and the sum of all the numbers in each row.
There are various ways to print the Pascal triangle, but today I am going to share with you a very simple and effective method.
Method:
Step 1: Define a function to print the Pascal triangle.
Here is the code:
void pascal(int n)
{
int i,j;
printf("\n");
for (i=0; i<n; i++)<="" p=""></n;>
{
printf("\n");
for (j=1; j<=n; j++)
printf("%d", i*j);
}
}
Step 2: Call the function and pass the number of rows as an argument.
pascal(5);
The output will be:
1
1
1
1
1
Step 3: Define another function to reverse the triangle.
Here is the code:
void reverse(int n)
{
int i,j,k;
for (i=0; i<n; i++)<="" p=""></n;>
{
printf("\n");
for (j=1; j<=n; j++)
printf("%d", j*i);
}
}
Step 4: Call the function and pass the number of rows as an argument.
reverse(n);
Output:
1
2
3
4
5
Step 5: Define another function to find the sum of all the elements in the triangle.
Here is the code:
int sum(int n)
{
int s=0,i;
for (i=0; i<n; i++)<="" p=""></n;>
s+=i;
return s;
}
Step 6: Call the function and pass the number of rows as an argument.
sum(n);
Output:
15
Note: This is just a small example of Pascal’s triangle and there are many ways to print the triangle.
We know that a triangle is an arrangement of numbers in rows and columns. In this case, we are talking about a Pascal triangle. In the below program, we are printing the first triangle from 0 to 1.
First of all, we have to declare an array of size 9. An array is nothing but a series of memory locations that we use to store data. In this program, we have declared a variable named ‘a’ of size 9. Now we are going to declare another variable named ‘b’ of size 2.
Now we are going to print the value from ‘0’ to ‘1’. In this case, we are going to print from the number ‘0’ to ‘1’. To print the number from 0 to ‘1’, we are using the loop condition. For the sake of simplicity, we have used the loop condition of I>0. If you use a loop condition of I <=1, then you will get the first triangle printed.
To print the value of the number ‘i’ in a row, we are using the nested loop. The first level will print the values from ‘0’ to ‘1’. And the second level will print the values from ‘1’ to ‘2’. In this way, the nested loop will run from ‘0’ to ‘9’.
To print the next triangle, we are using the nested loop again. Here we are taking two variables as input, one is a and another is b. The first level will print the values from ‘0’ to ‘1’. And the second level will print the values from ‘1’ to ‘2’. In this way, the nested loop will run from ‘0’ to ‘9’.
Finally, we are closing the loop. The outer loop is getting executed from ‘0’ to ‘9’. And the inner loop is getting executed from ‘0’ to ‘1’. So, the total execution time is ‘10*10=100’.
I hope this tutorial has helped you to understand how to print the Pascal triangle. If you have any doubts or suggestion then let me know in the comments section.