Signup/Sign In

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

  1. Start
  2. Declare a variable say rows to store the number of rows entered by the user.
  3. Initialize the number of rows.
  4. 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++)
  5. 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.
  6. Call a function to calculate the factorial.
  7. Inside the inner loop use formula term = fact(n) / (fact(k) * fact(n-k)); to print current term of pascal triangle.
  8. Display the pattern
  9. 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

  1. Start
  2. Declare the number of rows
  3. Initialize the number of rows
  4. Use three for loops to print the pattern
  5. Use the first for loop to iterate till all the rows
  6. Use the second for loop to print the spaces
  7. Use the third for loop to print the pattern
  8. Display the pattern
  9. 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

  1. Start
  2. Declare the number of rows
  3. Initialize the number of rows
  4. Call a function to print the pattern.
  5. Use three for loops to print the pattern
  6. Use the first for loop to iterate till all the rows
  7. Use the second for loop to print the spaces
  8. Use the third for loop to print the pattern
  9. Display the pattern
  10. 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



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight