Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pascal's triangle in c with recursive functions

Tags:

c

Hi this is my code for calculating pascal triangle but it runs error : has stopped working... why ? i think its error is in paskal function

#include <stdio.h>

long paskal(int,int);

int main (void)
{
    int  n = 0 ;
    int m = 0 ;
    int k  = 0 ;

    scanf("%d" , &n);

    for(k = 1  ; n >= k ;   )
    {
        for(  m = 1 ; k >= m   ; m++ )
        {
            long f = paskal(k , m ) ;
            printf("%ld" , f);
        }
        printf("\n");
        k++;
    }

     return 0; 
}

long paskal( int n , int i ) 
{
    if(n == 1 && i == 1 )
        return 1 ; 
    else
        return paskal(n-1,i) + paskal(n-1,i-1);
}
like image 623
S.Fadaei Avatar asked Mar 07 '23 13:03

S.Fadaei


1 Answers

The limit-conditions are not correct.

The correct way to put the limit conditions is

if(n == 1 || i == 1 )
    return 1 ; 
else
    ....
like image 64
alinsoar Avatar answered Mar 20 '23 17:03

alinsoar