×

Pascal Triangle in C

  • The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.
  • In a pascal triangle, every number is obtained by summing the two numbers above it.

Example:

Pascal Triangle in C
  • There are two different ways to obtain the pascal triangle in c, They are
  • Using 2d array
  • Using combination

Using 2d Arrays

  • Here every number is the sum of the above two numbers, To store the values obtained, we create a 2d array.
  • To obtain a value,we use stored values from the array.
Pascal Triangle in c
  • The time complexity for generating the pascal triangle is O(n2).
  • The space complexity for generating the pascal triangle is O(1).

Program:

/ / program to print pascal triangle
/ / using 2d array method
#include <stdio.h>
/ / pascal function
Void pascal ( int n )
{
/ / defining a array to store
Int a [n] [n];
for ( inti = 0 ; i< n; i++)
{
for ( int j = 0;j <= i; j++)
{
If ( i == j | | j == 0)
a [i] [j] = 1;
else
a [i] [j] = ( a [i -1][j – 1] + a [i -1][j];
printf(“%d ” , a [i][j] );
}
Printf( “\n” );
} }
/ / main method
int main ( ) {
int n = 5;
pascal (n);
return 0;
}

Output:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

2. combination method:

  • In this method, C(n,r) is used to represent the nth row of nth element in pascal triangle.
  • The formula to calculate is :

C(n,r) = C(n, r - 1) * (n – r + 1 ) / r

Program:

#include <stdio.h>
 Void pascal ( int n )
{
for ( inti = 1;i <= n;i++)
{
Int c = 1;
for ( int j = 1; j <= i; j++ )
 {
Printf( “ %d ”, c);
 c = c * (i – j) / j; 
  }
Printf(“ \n”);  }  }
/ /  main method
int main ( ) {
int n = 5;
pascal (n);
return 0;
}


Output:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
  • Here the element in the pascal triangle is calculated in O(1) time using the above formula.
  • The time complexity for generating the pascal triangle is O(n2).
  • The space complexity for generating the pascal triangle is O(1).

Related Topics

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.