C Program to Display Pascal triangle

This C Program displays pascal triangle.

Here is source code of the C Program to display Pascal triangle.The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Display Pascal triangle
  3.  */
  4. #include <stdio.h>
  5.  
  6. void main()
  7. {
  8.     int array[15][15], i, j, rows, num = 25, k;
  9.  
  10.     printf("\n enter the number of rows:");
  11.     scanf("%d", &rows);
  12.     for (i = 0; i < rows; i++)
  13.     {
  14.         for (k = num - 2 * i; k >= 0; k--)
  15.             printf(" ");
  16. 	    for (j = 0; j <= i; j++)
  17. 	    {
  18.                 if (j == 0 || i == j)
  19. 		{
  20.                     array[i][j] = 1;
  21.                 }
  22.                 else
  23.                 {
  24.                     array[i][j] = array[i - 1][j - 1] + array[i - 1][j];
  25. 		}
  26. 		printf("%4d", array[i][j]);
  27.             }
  28.             printf("\n");
  29.     }
  30. }


Output:
$ cc pgm37.c
$ a.out
 
enter the number of rows:2
                             1
                           1   1

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

If you wish to look at other example programs on Mathematical Functions, go to C Programming Examples on Mathematical Functions. If you wish to look at programming examples on all topics, go to C Programming Examples.
Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

Subscribe Newsletter & Posts
Follow Manish & Sanfoundry