C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print Pascal’s triangle (loop)

Introduction

Pascal’s Triangle is a triangular array of numbers where:

  • The first and last value in every row is 1.
  • Every other number is the sum of the two numbers directly above it.

For example, for n = 5, the pattern looks like:

      1

     1 1

    1 2 1

   1 3 3 1

  1 4 6 4 1

It is based on binomial coefficients:

C Programs

C Program: Print Pascal’s triangle (loop)

Method 1: Using for loop

C

#include <stdio.h>

 

long factorial(int n) {

    long f = 1;

    for (int i = 1; i <= n; i++)

        f *= i;

    return f;

}

 

int main() {

    int n, i, j;

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    for (i = 0; i < n; i++) {

        // Print leading spaces

        for (j = i; j < n - 1; j++)

            printf(" ");

 

        // Print values in the row

        for (j = 0; j <= i; j++)

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

 

        printf("\n");

    }

 

    return 0;

}

Output

 
OUTPUT :
Enter number of rows: 5
    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Explanation

  1. Factorial Function computes n!.
  2. Binomial Coefficient Formula → n! / (r! * (n - r)!).
  3. Outer loop → controls number of rows.
  4. Inner loops → print spaces and coefficients.

 

C Program: Print Pascal’s triangle (loop)

Method 2: Using while loop

C

#include <stdio.h>

 

long factorial(int n) {

    long f = 1;

    int i = 1;

    while (i <= n) {

        f *= i;

        i++;

    }

    return f;

}

 

int main() {

    int n, i = 0, j;

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    while (i < n) {

        j = i;

        while (j < n - 1) {

            printf(" ");

            j++;

        }

 

        j = 0;

        while (j <= i) {

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

            j++;

        }

 

        printf("\n");

        i++;

    }

 

    return 0;

}

Output

 
OUTPUT :
Enter number of rows: 5
    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Explanation

  1. Factorial Function computes n!.
  2. Binomial Coefficient Formula → n! / (r! * (n - r)!).
  3. Outer loop → controls number of rows.
  4. Inner loops → print spaces and coefficients.

 

C Program: Print Pascal’s triangle (loop)

Method 3: Using do..while loop

C

#include <stdio.h>

 

long factorial(int n) {

    long f = 1;

    int i = 1;

    do {

        f *= i;

        i++;

    } while (i <= n);

    return f;

}

 

int main() {

    int n, i = 0, j;

    printf("Enter number of rows: ");

    scanf("%d", &n);

 

    do {

        j = i;

        do {

            if (j < n - 1)

                printf(" ");

            j++;

        } while (j < n);

 

        j = 0;

        do {

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

            j++;

        } while (j <= i);

 

        printf("\n");

        i++;

    } while (i < n);

 

    return 0;

}

Output

 
OUTPUT :
Enter number of rows: 5
    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Explanation

  1. Factorial Function computes n!.
  2. Binomial Coefficient Formula → n! / (r! * (n - r)!).
  3. Outer loop → controls number of rows.
  4. Inner loops → print spaces and coefficients.