Generate Pascals triangle - C

WAP to generate Pascal's triangle.
#Assignment3b

==> Code goes like this: 

#include <stdio.h>
#define rows 6
int main()
{
    int cal = 1, space, i, j;
    for(i=0; i<rows; i++)            // outer loop for displaying rows
    {
        for(space=1; space <= rows-i; space++)  // space for every row starting
            printf("  ");

        for(j=0; j <= i; j++)     // inner loop for displaying the pascal triangle of numbers
        {
            if (j==0 || i==0)     // either outer loop value or inner-loop value is "0 " it prints 1
                cal = 1;
            else
                cal = cal*(i-j+1)/j;  //calculate the coefficient

            printf("%4d", cal);
        }
        printf("\n");
    }

    return 0;
}

Output:










If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment