C Program To Print Different Patterns
Here, we are given different patterns and our task is to print them in the given order. But before moving forward, if you are not familiar with the concept of loops in C, then do check the article on Loops in C.
Input: Enter the number of rows: 7
Output:
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
Program 1: Display Pascal's Triangle
In this method, we will use the factorial formula to print Pascal's triangle.
Algorithm
- Start
- Declare a variable say rows to store the number of rows entered by the user.
- Initialize the number of rows.
- Use a for loop to iterate through all the rows i.e., from 0 to rows. The row should look like for(int i=0;i<rows;i++)
- Use another loop inside the previous for loop to print the terms in Pascal's triangle. Initialize the loop from 0 that goes to i, increment 1 in each iteration.
- Call a function to calculate the factorial.
- Inside the inner loop use formula term = fact(n) / (fact(k) * fact(n-k)); to print current term of pascal triangle.
- Display the pattern
- Stop
Below is the code for the same.
/* C program to print Pascal triangle up to n rows */
#include <stdio.h>
long long fact(int n); // Function definition
int main()
{
int n, k, rows, i; //Declare variables
long long term;
printf("Enter number of rows : ");
scanf("%d", &rows); //Initialize the rows
printf("\n");
for(n=0; n<rows; n++)
{
for(i=n; i<=rows; i++) //Print 3 spaces
printf("%3c", ' ');
for(k=0; k<=n; k++) //Term for the rows
{
term = fact(n) / (fact(k) * fact(n-k)); //Function Call
printf("%6lld", term); //Print the terms
}
printf("\n");
}
return 0;
}
/* Function to calculate factorial */
long long fact(int n) //Function Definition
{
long long factorial = 1ll;
while(n>=1)
{
factorial *= n;
n--;
}
return factorial;
}
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
Program 2: Display Pascal's Triangle
In this method, we will use loops to print Pascal's triangle pattern.
Algorithm
- Start
- Declare the number of rows
- Initialize the number of rows
- Use three for loops to print the pattern
- Use the first for loop to iterate till all the rows
- Use the second for loop to print the spaces
- Use the third for loop to print the pattern
- Display the pattern
- Stop.
Below is the code for the same.
/*C Program to print Pascal's Triangle*/
#include <stdio.h>
int main()
{
int rows, coef = 1; //Row Declaration
printf("Enter the number of rows: ");
scanf("%d", &rows); //Initialize the rows
printf("\n");
for (int i = 0; i < rows; i++)
{
for (int k = 1; k <= rows - i; k++)
printf(" ");
for (int j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
Enter the number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Program 3: Display Pascal's Triangle
In this method, we will use functions to print Pascal's triangle.
Algorithm
- Start
- Declare the number of rows
- Initialize the number of rows
- Call a function to print the pattern.
- Use three for loops to print the pattern
- Use the first for loop to iterate till all the rows
- Use the second for loop to print the spaces
- Use the third for loop to print the pattern
- Display the pattern
- Stop.
Below is the code for the same.
#include <stdio.h>
void printPattern(int rows, int coef) //Function Definition
{
for (int i = 0; i < rows; i++)
{
for (int k = 1; k <= rows - i; k++)
printf(" ");
for (int j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
}
int main()
{
int rows, coef = 1; //Row Declaration
printf("Enter the number of rows: ");
scanf("%d", &rows); //Initialize the rows
printf("\n");
printPattern(rows,coef); //Function Call
return 0;
}
Enter the number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1