C

Inverted Pascal Triangle In C

In 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 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 C
#include <stdio.h>

int main()
{
    int f[20][20], rows;
	
    printf("Enter the number of lines: ");
    scanf("%d",&rows);

    for (int i = 0; i < rows; ++i)
        f[i][i] = f[i][0] = 1;
    for (int i = 2; i != rows; ++i)
        for (int j = 1; j != i; ++j)
            f[i][j] = f[i-1][j] + f[i-1][j-1];

		
    for (int i = rows - 1; i >= 0; --i)
    {
        for (int j = 0; j < rows - i - 1; ++j)
            printf("%4c", ' ');
        for (int j = 0; j <= i; ++j)
            printf("%4d%4c", f[i][j], ' ');
        printf("\n");
    }
    return 0;
}

Output:

Enter the number of lines: 7

   1       6      15      20      15       6       1    
       1       5      10      10       5       1    
           1       4       6       4       1    
               1       3       3       1    
                   1       2       1    
                       1       1    
                           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 *