C program to print Pascal Triangle without using function

Before going to the program for Pascal Triangle first let us understand what is a Pascal Triangle?

Pascal Triangle:

                     Pascal triangle is a triangular array of the binomial coefficients.

Program code for Pascal Triangle in C:

/* Program for Pascal triangle */

#include<stdio.h>
#include<conio.h>

void main()
{
    int i,j,k,n,c;
    clrscr();
    
    printf("\n Enter the limit: ");
    scanf("%d",&n);
    printf("\n ");
    
    for(i=0;i<n;i++)
    {
        c=1;
        for(k=0;k<n-i;k++)
        {
            printf(" ");
        }
        for(j=0;j<=i;j++)
        {
            printf(" %d ",c);
            c=(c*(i-j)/(j+1));
        }
        printf("\n");
    }
    getch();
}

Working:

  • First the computer reads the value of the limit from the user.
  • Then using the loop the value of c and the spaces required are printed.
  • Finally we will be getting the pascal triangle.

Step by Step working of the above Program Code:

  1. Let us assume the value of limit as 4.
  2. It assigns n=4.
  3. It assigns i=0 and the for loop continues until the condition i<n is true

3.1.  i<n   (0<4)   for loop condition is true

It assigns c=1.

Then it assigns k=0 and the for loop continues until the condition k<n-i is true

(k<4), So the for loop continues for 4 iterations and prints 4 blank spaces

Then it assigns j=0 and the for loop continues until the condition j<=i is true

j<=i   (0<=1)   for loop condition is true

It prints 1(value of c).

c=(c*(i-j)/(j+1))   (c=(1*(1-0)/0+1)))   So, c=1

j++   So, j=1

j<=i   (1<=0)   for loop condition is false

So it comes out of the for loop.

i++  So, i=1

3.2.  i<n   (1<4)   for loop condition is true

It assigns c=1.

Then it assigns k=0 and the for loop continues until the condition k<n-i is true

(k<3), So the for loop continues for 3 iterations and prints 3 blank spaces

Then it assigns j=0 and the for loop continues until the condition j<=i is true

j<=i   (0<=1)   for loop condition is true

It prints 1(value of c).

c=(c*(i-j)/(j+1))   (c=(1*(1-0)/0+1)))   So, c=1

j++   So, j=1

j<=i   (1<=1)   for loop condition is true

It prints 1(value of c).

c=(c*(i-j)/(j+1))   (c=(1*(1-1)/1+1)))   So, c=0

j++   So, j=2.

j<=i   (2<=1)   for loop condition is false

So it comes out of the for loop.

i++  So, i=2

3.3.  i<n   (2<4)   for loop condition is true

It assigns c=1.

Then it assigns k=0 and the for loop continues until the condition k<n-i is true

(k<2), So the for loop continues for 2 iterations and prints 2 blank spaces

Then it assigns j=0 and the for loop continues until the condition j<=i is true

j<=i   (0<=2)   for loop condition is true

It prints 1(value of c).

c=(c*(i-j)/(j+1))   (c=(1*(2-0)/0+1)))   So, c=2

j++   So, j=1

j<=i   (1<=2)   for loop condition is true

It prints 2(value of c).

c=(c*(i-j)/(j+1))   (c=(2*(2-1)/1+1)))   So, c=1

j++   So, j=2

j<=i   (2<=2)   for loop condition is true

It prints 1(value of c).

c=(c*(i-j)/(j+1))   (c=(1*(2-2)/2+1)))   So, c=0

j++   So, j=3

j<=i   (3<=2)   for loop condition is false

So it comes out of the for loop.

i++  So, i=3

3.4.  i<n   (3<4)   for loop condition is true

It assigns c=1.

Then it assigns k=0 and the for loop continues until the condition k<n-i is true

(k<1), So the for loop continues for 1 iteration and prints 1 blank space

Then it assigns j=0 and the for loop continues until the condition j<=i is true

j<=i   (0<=3)   for loop condition is true

It prints 1(value of c).

c=(c*(i-j)/(j+1))   (c=(1*(3-0)/0+1)))   So, c=3

j++   So, j=1

j<=i   (1<=3)   for loop condition is true

It prints 3(value of c).

c=(c*(i-j)/(j+1))   (c=(3*(3-1)/1+1)))   So, c=3

j++   So, j=2

j<=i   (2<=3)   for loop condition is true

It prints 3(value of c).

c=(c*(i-j)/(j+1))   (c=(3*(3-2)/2+1)))   So, c=1

j++   So, j=3

j<=i   (3<=3)   for loop condition is true

It prints 1(value of c).

c=(c*(i-j)/(j+1))   (c=(1*(3-3)/3+1)))   So, c=0

j++   So, j=4

j<=i   (4<=3)   for loop condition is false

So it comes out of the for loop.

i++  So, i=4

3.5.  i<n   (4<4)   for loop condition is false.

So it comes out of the for loop.

  1. Thus the program execution is completed.

Output:

pascal triangle

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

You may also like...

1 Response

Leave a Reply

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