Pascal Triangle

/* Program for print pascal triangle using Array
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[20][20],i,j,rows,k;
    clrscr();

    printf("\n enter the number of rows:");
    scanf("%d",&rows);

    for(i=0;i<rows;i++)
    {
        for(j=0;j<=i;j++)
        {
            if(j==0||i==j)
            {
                a[i][j]=1;
            }
            else
            {
                a[i][j] = a[i-1][j-1] + a[i-1][j];
            }
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    getch();
}
Click Here for Download

//Write a program to print the pascal triangle without using array
/* 
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
 */
# include <stdio.h> 
# include <conio.h>
void main()
{
    int a, p, q, r, x ;
    clrscr();
    a = 1 ;
    q = 0 ;
    printf("Enter the number of rows : ") ;
    scanf("%d", &p) ;

    printf("\nPascal's triangle is : \n\n") ;
    while (q < p)
    {
        for(x = 0 ; x <= q ; x++)
        {
            if((x == 0) || (q == 0))
                a = 1 ;

            else
                a = (a * (q - x + 1)) / x ;

            printf("%d ", a) ;
        }
        printf("\n\n") ;
        q++;
    }
    getch() ;
}
Click Here for Download 

No comments:

Post a Comment