Question:

Printing pascal triangle in C?

by  |  earlier

0 LIKES UnLike

 Tags:

   Report

1 ANSWERS


  1. the logic i have used is nCr => n!/(r!*(n-r)!)

    for example the outer loop counter is 'n' and the inner loop counter is 'r' , then the corresponding element of the pascal's triangle will be nCr.

    keep in mind that both the loops will have to start from zero.

    #include<stdio.h>

    main()
    {
    int num,i,j,k,space;
    int difffact=1,diff,n,r,x,y,comb=0,nfact=1,rfact=1;
    printf("please enter the number of lines\n");
    scanf("%d",&num);

    k=num-1;
    space=num-1;
    for(i=0;i<num;i++)
    {
    k=space--;
    for(;k>0;k--)
    {
    printf(" ");
    }
    for(j=0;j<=i;j++)
    {
    comb=0;
    nfact=1;
    rfact=1;
    difffact=1;

    for(n=i;n>=1;n--)
    nfact=nfact*n;

    for(r=j;r>=1;r--)
    rfact=rfact*r;

    diff=i-j;
    for(;diff>=1;diff--)
    difffact=difffact*diff;

    comb=(nfact/(rfact*difffact));
    printf("%d ",comb);
    }
    printf("\n");
    }
    }

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions