C

Pascal Triangle in C Using For Loop

In this tutorial, we are going to see how to display pascal triangle in C using for loop. 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 For Loop
#include <stdio.h>

void main()
{
    int nbr, space, i, j, p=1;
  
    printf("Enter the number of lines: ");
    scanf("%d",&nbr);
  
    for(i = 0; i < nbr; i++)
    {
        for(space = 1; space <= nbr-i; space++)
            printf("  "); //add space
        for(j = 0; j <= i; j++)
        {
            if ( i==0 || j==0 )
                p = 1;
            else
               p = p*(i-j+1)/j;
            printf("% 4d",p);
        }
        printf("\n");
    }
}

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 *