More information about the Binomial Theorem

(x + y)n =
n
k=0
(
n
k
)
xk yn-k

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

 
(
 
 
n
k
 
 
) is pronounced “n choose k”.
 

The following C code is an implementation of the binomial theorem. This code was tested on Windows 10 with Microsoft Visual Studio Community 2015.

#include "stdafx.h"
#include "math.h"
#include "stdio.h"

int n_choose_k(int n, int k);
int factorial(int x);

int main()
{
	int n;
	int k;

	printf("Enter a value for n: ");
	scanf_s("%d", &n);
	/* Summation from k=0 to n */
	for (k = 0; k <= n; k++)
	{
		printf("%dx^%dy^%d+", n_choose_k(n, k), k, n-k);
	}

	return 0;
}

/* n!/k!(n-k)! */
int n_choose_k(int n, int k)
{
	return factorial(n) / (factorial(k) * factorial(n - k));
}

int factorial(int x)
{
	int value = 1;
	while (x > 1)
	{
		value *= x * (x - 1);
		x = x - 2;
	}
	return value;
}

More information about the Binomial Theorem