Q:

Pascal triangle in C

0

Pascal triangle in C

C program to print the Pascal triangle that you might have studied while studying Binomial Theorem in Mathematics. A user will enter how many numbers of rows to print. The first four rows of the triangle are:

   1
  1 1
 1 2 1
1 3 3 1

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
 
long factorial(int);
 
int main()
{
  int i, n, c;
 
  printf("Enter the number of rows you wish to see 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 ",factorial(i)/(factorial(c)*factorial(i-c)));
 
    printf("\n");
  }
 
  return 0;
}
 
long factorial(int n)
{
  int c;
  long result = 1;
 
  for (c = 1; c <= n; c++)
    result = result*c;
 
  return result;
}

output:

Enter the number of rows you wish to see in pascal triangle

4

   1 

  1 1 

 1 2 1 

1 3 3 1 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now