×

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

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 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.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 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.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.