Showing posts with label pascal triangle. Show all posts
Showing posts with label pascal triangle. Show all posts

Monday, 23 September 2013

Friday, 20 September 2013

Showing posts with label pascal triangle. Show all posts
Showing posts with label pascal triangle. Show all posts

Monday, 23 September 2013

Program to print Pascal Triangle


C program to print Pascal triangle using for loop

#include<stdio.h>
long fact(int);
int main(){
    int line,i,j;

    printf("Enter the no. of lines: ");
    scanf("%d",&line);

    for(i=0;i<line;i++){
         for(j=0;j<line-i-1;j++)
             printf(" ");

         for(j=0;j<=i;j++)
             printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
         printf("\n");
    }
    return 0;
}

long fact(int num){
    long f=1;
    int i=1;
    while(i<=num){
         f=f*i;
         i++;
  }
  return f;
 }

Output:

Enter the no. of lines: 8
       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
1 7 21 35 35 21 7 1


Friday, 20 September 2013

Display Pascal Triangle in C


C program to display Pascal Triangle

#include<stdio.h>  
main()
{
    int i,j,k[50],l[50][50],m;
    printf("Enter the number of rows.\n");
    scanf("%d",&m);
    for(i=1;i<=m;i++)  
    {  
        for(j=0;j<(m-i);j++)  
        printf("   ");  
        for(j=0;j<i;j++)  
        {  
            k[0]=1;  
            if(j==i-1)  
            k[j]=1;  
            l[i][j]=k[j];  
            k[j+1]=l[i-1][j]+l[i-1][j+1];  
            printf("%5d ",k[j]);  
        }  
        printf("\n");
    }
}


Executing the program....
$demo
Enter the number of rows.
                1 
             1     1 
          1     2     1 
       1     3     3     1 
    1     4     6     4     1