C

Pascal Triangle in C Using Array

In this tutorial, we are going to see how to display pascal triangle in C using array. Pascal’s triangle can be constructed by first placing a 1 along the left and right edges. Then the triangle can be filled from the top by adding the two numbers just above to the left and right of each position in the triangle.


 
There are five ways to print pascal triangle in C, by using for loop, array, factorial, recursion, or by creating a function.
Inverted Pascal Triangle In CInverted Pascal Triangle In CIn this tutorial, we are going to see how to write a program that create an inverted pascal triangle in C. Pascal’s triangle can be…Read More  
 

Pascal Triangle in C Using Array
#include <stdio.h>

int main()
{
    int arr[100][100];
    int i, j, n, space;

    printf("Enter the number of lines: ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        for (space = 0; space < n - 1 - i; ++space)
            printf(" ");

        for (j = 0; j <= i; ++j) {
            if (j == 0 || j == i)
                arr[i][j] = 1;
            else
                arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Output:

Enter the number of lines: 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
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *