-1

I am trying to output a left-justified Pascal’s triangle. The program should first read one positive integer N from the user via a prompt “N:”. Then, the program will print the first N rows of the Pascal’s triangle on the screen. However, I think that there is a problem with my outer two for-loops. Instead of getting (N=3)
1
1,1
1,2,1

I am getting
1
1,2
2,4,4


#include <stdio.h>
int main(void) {

    int input,i,j,k,p,N,x;
    int f1=1;
    int f2=1;
    int f3 = 1;

    printf("N:");
    scanf("%d",&N);

    for(i=1;i<=N;i++){
        for(j=1;j<=i;j++){
            for(x = 1; x<= N-1; x++){
                f1 = f1 * x;
            }
            for(x = 1; x <= j-1; x++){
                f2 = f2 * x;
            }
            for(x = 1; x <= N-j; x++){
                f3 = f3 * x;
            }
            p= (f1)/(f2*f3);

            if(j==i)
                printf("%d",p);
            else
                printf("%d,",p);
        }
        printf("\n");

    }



    return 0;
}
hyde
  • 60,639
  • 21
  • 115
  • 176
bow_one
  • 11
  • 1
  • 4
  • Welcome to Stack Overflow! Kindly format your question so it becomes more readable. – Sourav Ghosh Feb 04 '15 at 06:17
  • This doesn't look like a question to me. – Jens Gustedt Feb 04 '15 at 06:19
  • I suggest you check return value of `scanf("%d",&N);`, because if that has parse error, `N` will be left uninitialized. – hyde Feb 04 '15 at 06:22
  • To solve your problem, try running under debugger, and observing values of `f1`, `f2` and `f3` in the rows where you get wrong output... (Or, if you are unable or unwilling to use a debugger, add debug prints for the same purpose.) – hyde Feb 04 '15 at 06:30

2 Answers2

1

Pascal's Triangle formula: C(n,k), nCk = n!/(k! * (n-k)!)
Try this code:

#include <stdio.h>
int main(void) {
    int i,j,p,N,x;
    int f1=1;
    int f2=1;
    int f3 = 1;

    printf("N:");
    scanf("%d",&N);

    for(i=0;i<=N;i++){      
        for(j=0;j<=i;j++){
            f1=f2=f3=1;  //after each calculation change value to default otherwise it will take old value.
            for(x = 1; x<= i; x++){ // run upto x<=i
                f1 = f1 * x;
            }
            for(x = 1; x <= j; x++){ //run upto x<=j
                f2 = f2 * x;
            }
            for(x = 1; x <= i-j; x++){ //run upto x<=i-j
                f3 = f3 * x;
            }
            p= (f1)/(f2*f3);

            if(j==i)
                printf("%d",p);
            else
                printf("%d,",p);
        }
        printf("\n");

    }
return 0;
}

As after calculating one value you are not changing it to default, so its doing next calculation using old values and giving wrong output.
so use f1=f2=f3=1; after each calculation.

Himanshu
  • 4,327
  • 16
  • 31
  • 39
0

You need to set f1, f2, and f3 to 1 on each iteration, not just for the first iteration. You also need to adjust the loop bounds.

#include <stdio.h>

int main(void)
{
    int N;

    printf("N: ");
    scanf("%d", &N);

    printf("Pascal triangle for N = %d\n", N);

    for (int i = 0; i <= N; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            int f1 = 1;
            int f2 = 1;
            int f3 = 1;
            for (int x = 1; x<= i; x++)
                f1 *= x;
            for (int x = 1; x <= j; x++)
                f2 *= x;
            for (int x = 1; x <= i-j; x++)
                f3 *= x;
            int p = (f1)/(f2*f3);

            if (j == i)
                printf("%d", p);
            else
                printf("%d,", p);
        }
        printf("\n");
    }

    return 0;
}

Example output:

$ ./pascal
N: 7
Pascal triangle for N = 7
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
$
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thanks everyone here for all your help. I am new to this community, thus I still need to work on asking properly formatted questions. – bow_one Feb 04 '15 at 12:56