C Program to Generate Pascal Triangle using 1 D Array

«
»
This C Program generates pascal triangle 1 dimensional array.

Here is source code of the C Program to generate pascal triangle 1 dimensional array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Generate Pascal Triangle 1 D Array
  3.  */
  4. #include <stdio.h>
  5.  
  6. void main()
  7. {
  8.     int array[30], temp[30], i, j, k, l, num;           //using 2 arrays
  9.  
  10.     printf("Enter the number of lines to be printed: ");
  11.     scanf("%d", &num);
  12.     temp[0] = 1;
  13.     array[0] = 1;
  14.     for (j = 0; j < num; j++)
  15.         printf(" ");
  16.     printf(" 1\n");
  17.     for (i = 1; i < num; i++)
  18.     {
  19.         for (j = 0; j < i; j++)
  20.             printf(" ");
  21.         for (k = 1; k < num; k++)
  22.         {
  23.             array[k] = temp[k - 1] + temp[k];      
  24.         }
  25.         array[i] = 1;
  26.         for (l = 0; l <= i; l++)
  27.         {
  28.             printf("%3d", array[l]);
  29.             temp[l] = array[l];
  30.         }
  31.         printf("\n");
  32.     }
  33. }

$ cc pgm69.c
$ a.out
Enter the number of lines to be printed: 4
     1
   1  1
 1  2  1
1  3  3  1

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.