Download Quick Currency Converter


Pascal Triangle

Description:Remember pascal triangle from mathematics(binomial theorem)? If no, check out the wikipedia description of pascal triangle. This project generates pascal triangle for a given number of rows. Note that for small number of rows, the shape of the pascal triangle is pretty much retained, but for large input(>10), it gets desturbed; thus avoid large inputs.

Difficulty: Easy/Medium

Language: C++

Compiler/IDE: Visual Studio, dev C++ (others will work as well)



View Source

            							
#include<stdio.h>
#include<math.h>

int fact(int);
void space(int);

int main() 
{
	int row, n,r;
	printf("Enter Number of rows\n");
	scanf("%d" , &row);
	
	for (n = 0; n<= row; n++ ) 
	{
		space(row - n);
		for (r = 0; r<=n; r++ )
		{
			printf("%d " , fact(n)/( fact(n-r) * fact(r)));
			printf("  ");
			
		}

		printf("\n");
	}
   
	printf("\nEnter any character to exit...\n");
	scanf("%d", &row);      //Stop the screeen...

}

int fact(int n) 
{
	int prod = 1, i;
	for ( i = n; i>= 2; i--) {
		prod = prod * i;

	}
	return prod;

}

void space(int dim)
{
     int i;
	for (i = 1; i<= dim; i++ )
	{
		printf("  ");
	}

}


Notes:The project uses the following formula for finding a binomial coefficent: fact(n)/( fact(n-r) * fact(r)). The function for fact(factorial) is provided in the code. Note that in contrast to our other project: Factorial of a number that uses recursion to calculate factorial, this project uses iteration method. The "space" function is used for displaying a proper layout for the pascal triangle.