Tuesday, 26 December 2017

Pascal Triangle Program in C?

To Print Pascal Triangle in C
#include<stdio.h>
#include<conio.h>
long fact(int m); //function declaration

long fact(int  m)  // function definition
{
    int c;        // c is counter variable
    long result = 1;
   
    for( c = 1 ; c <= m ; c++ )
            result = result*c;
            return result;
}

void main()
{
    Int  i, n, c;
    clrscr();
    printf("How many rows you want to enter in pascal triangle?\n");
   
    scanf("%d",&n);
   
    for ( i = 0 ; i < n ; i++ )
    {
        for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
        printf(" ");
        for( c = 0 ; c <= i ; c++ )
            printf("%ld ",fact(i)/(fact(c)*fact(i-c)));
            printf("\n");
    }
    getch();
}

OUTPUT:

How many rows you want to enter in pascal triangle?                                                                            
5                                                                                                                              
    1                                                                                                                          
   1 1                                                                                                                         
  1 2 1                                                                                                                        
 1 3 3 1 
1 4 6 4 1

Note:
Pascal's triangle is a set of numbers arranged in the form of a triangle. Each number in a row is the sum of the left number and right number on the above row. If a number is missing in the above row, it is assumed to be 0. The first row starts with number 1.

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...