Hello and welcome to Coding in C. In this series, we will examine various simple code snippets and understand the basic concepts behind them. We will be covering a wide area, from recreational mathematics, network programming to database management. These tutorials are aimed towards novice programmers who are already familiar with the C language.

I basically code in a Windows environment (Win64) but I have made all the programs discussed ANSI C (C11) standard compatible, so you will not have any issues in building the programs on any complier. In case you do encounter an error when compiling in either platforms, pleas contact me. I have included the complete .C source file for your convenience, but however I suggest that you learn the concept and try out by yourself.

As a debut, I will discuss a program to display Pascal's Triangle. Basically, a Pascal's Triangle is a triangular construct where each number in the triangle is the sum of the two directly above it, like shown below.

Code

The following program will display Pascal's Triangle up to 13 rows. It consists of two functions, main() and fac().

- Code: Select all
`#include <stdio.h>`

long fac(int);

int main(void) {

int num, spaces, n, k;

long sum;

printf ("NO. OF ROWS:");

scanf ("%d", &num);

for (n = 0; n <= num - 1; n++){

for (spaces = num - n; spaces > 0; spaces--){

printf (" ");

}

for (k = 0; k <= n; k++){

sum = fac(n)/(fac(k) * fac(n-k));

printf ("%ld ",sum);

}

printf ("\n");

}

return 0;

}

long fac(int a){

long prod;

if (a >1){

prod = a * fac(a - 1);

return prod;

}

else

return 1;

}

Discussion

The n

^{th}row will contain n number of elements and we will call each element k such that k = 0, 1, 2,...(n-1) and any given element in any row is (n k). For example the 5

^{th}row will have 5 elements, each element designated as (5 0), (5 1), (5 2), (5 3) and (5 4).

The first element at the tip is designated (0 0).

To calculate any given (n k) element we use the formula:

(n k) = n! / (k!(n-k)!)

We implement this calculation by:

sum = fac(n)/(fac(k) * fac(n-k));

The function fac(); will recursively calculate the factorial.

This program will not display rows greater than 13 correctly. This is because the factorial is stored in a long variable. A signed long variable can hold upto 2,147,483,647 and an unsigned long variable can hold upto 4,294,967,295. Since 13! = 6,227,020,800, 13 rows would be the maximum we can display correctly.

In this program I have followed a rudimentary approach in spacing and formatting hence the triangle will go askew with higher rows.

End

Your expected output would be something similar:

For those of you who are interested in using computer programming for recreational mathematics, Project Euler is highly recommended.

This concludes Part 1 of Coding with C series and in the subsequent parts we will examine more programs. Your questions, suggestions are feedback are extremely welcome. For further information, contact me on Facebook. මෙම අඩවියේ ලියාපදිංචි වූ භාවිතාකරුවෙක් මෙම ලිපිය සිංහල භාෂාවට පරිවර්තනය කර නැවත මෙහි පළ කරන්නේ නම් ස්තුතිවන්ත වෙමි.