Pages

Monday

Program to Generate Pascal's Triangle in C | C Program

Here's a C program to generate Pascal's triangle with output. This program makes use of C concepts like For loop, While loop, IF-Else Condition, Nested Loops etc.


Pascal's Triangle


# include <stdio.h>
# include <conio.h>
void main() 
{ 
 int b, p, q, r, x ; 
 clrscr() ; 
 b = 1 ; 
 q = 0 ; 
 printf("Enter the number of rows : ") ; 
 scanf("%d", &p) ; 
 printf("\nPascal's triangle is : \n\n") ; 
 while (q < p) 
 { 
  for(r = 40 - 3 * q ; r > 0 ; --r) 
   printf(" ") ; 
  for(x = 0 ; x <= q ; ++x) 
  { 
   if((x == 0) || (q == 0)) 
    b = 1 ; 
   else 
    b = (b * (q - x + 1)) / x ; 
   printf("%6d", b) ; 
  } 
  printf("\n\n") ; 
  ++q ; 
 } 
 getch() ; 
} 

Output of above program is 

Enter the number of rows : 5

Pascal's triangle is :

        1

      1  1

    1  2  1

  1  3  3  1

1  4  6  4  1

4 comments:

  1. can you please provide a code which can print the following pattern
    1
    232
    34543
    4567654
    567898765
    4567654
    34543
    252
    1

    ReplyDelete
  2. Take a look at this program

    http://cprogramming.language-tutorial.com/2013/06/program-to-print-number-pattern-in-c.html

    Hope it helps... :)

    ReplyDelete
  3. Pascal triangle is the basic lab c program used in many institutions. Thank you for sharing this program.
    regards:
    srinath reddy.
    admin of Programming Tutorials for Beginners

    ReplyDelete

  4. Hola,


    Great post. Well though out. This piece reminds me when I was starting out c++ after graduating from college.

    I am working LDPC encoding and decoding for mini project work. With my effort i have completed LDPC encoding but struggling with LDPC decoding. I need to complete this project within short period so please help me.

    I don`t know how to start decoding algorithm in C language but i do have algorithm. So, can anyone help me in writing C language for decoding procedure.

    Thank you very much and will look for more postings from you.


    Thank you,

    ReplyDelete