Print given row in pascal triangle

The problem at hand is to print the elements of a given row in Pascal's triangle. Pascal's triangle is a mathematical structure where each number is the sum of the two numbers directly above it. It is a triangular array of binomial coefficients.

Problem Statement

Given an integer n, we want to print the elements of the n-th row in Pascal's triangle.

Example

Let's consider a few examples to illustrate the problem:

• For n = 5, the 5th row of Pascal's triangle is [1, 5, 10, 10, 5, 1].
• For n = 1, the 1st row of Pascal's triangle is simply [1].
• For n = 7, the 7th row is [1, 7, 21, 35, 35, 21, 7, 1].

Idea to Solve

To print the elements of a specific row in Pascal's triangle, we can use the binomial coefficient formula. The binomial coefficient C(n, k) represents the number of ways to choose k elements from a set of n elements, disregarding the order.

The formula for calculating C(n, k) is:

C(n, k) = n! / (k! * (n - k)!)

where n! represents the factorial of n.

We iterate through the elements of the row and calculate each element using the binomial coefficient formula. The first element of each row is always 1. The elements are calculated using the previous element and the formula.

Pseudocode

function pascal_triangle_row(n):
if n <= 0:
return
print "Row", n
k = 1
for j from 0 to n:
if j == 0 or n == 0:
k = 1
else:
k = k * (n - j + 1) / j
print k, "\t"
print newline

function main():
pascal_triangle_row(5)
pascal_triangle_row(1)
pascal_triangle_row(7)
pascal_triangle_row(9)

main()

Algorithm Explanation

1. The pascal_triangle_row function takes an integer n as input.
2. It first checks if n is less than or equal to 0. If so, it returns, as there is no valid row with a non-positive index.
3. It then prints the header for the current row.
4. It initializes a variable k to 1, which will be used to calculate the elements of the row.
5. It uses a loop to iterate through the row's elements.
6. Inside the loop, it checks if j (the current column) is 0 or n is 0. If so, it sets k to 1.
7. Otherwise, it calculates the next element using the binomial coefficient formula.
8. After calculating the element, it prints the value of k followed by a tab.
9. Finally, it prints a newline to move to the next line.

Code Solution

/*
C Program
Print given row in pascal triangle
*/
#include <stdio.h>

// Print the elements of given nth row in pascal triangle
void pascal_triangle_row(int n)
{
if (n <= 0)
{
return;
}
printf("\n Row %d \n  ", n);
//Loop controlling variables
int i = n, j = 0, k = 0;
for (j = 0; j <= n; j++)
{
if (j == 0 || i == 0)
{
//Set initial values
k = 1;
}
else
{
//Pascal's triangle number calculation
k = k * (i - j + 1) / j;
}
//Display the value of calculate number
printf("%d\t", k);
}
printf("\n");
}
int main()
{
//Test Cases
pascal_triangle_row(5);
pascal_triangle_row(1);
pascal_triangle_row(7);
pascal_triangle_row(9);
return 0;
}

Output

Row 5
1	5	10	10	5	1

Row 1
1	1

Row 7
1	7	21	35	35	21	7	1

Row 9
1	9	36	84	126	126	84	36	9	1
/*
Java program
Print given row in pascal triangle
*/
class PascalTriangle
{
// Print the elements of given nth row in pascal triangle
public void pascal_triangle_row(int n)
{
if (n <= 0)
{
return;
}
System.out.print("\n Row " + n + " \n ");
//Loop controlling variables
int i = n, j = 0, k = 0;
//print row
for (j = 0; j <= n; j++)
{
if (j == 0 || i == 0)
{
//Set initial values
k = 1;
}
else
{
//Pascal's triangle number calculation
k = k * (i - j + 1) / j;
}
//Display the value of calculate number
System.out.print("" + k + "\t");
}
System.out.print("\n");
}
public static void main(String[] args)
{
PascalTriangle obj = new PascalTriangle();
//Test Cases
obj.pascal_triangle_row(5);
obj.pascal_triangle_row(1);
obj.pascal_triangle_row(7);
obj.pascal_triangle_row(9);

}
}

Output

Row 5
1	5	10	10	5	1

Row 1
1	1

Row 7
1	7	21	35	35	21	7	1

Row 9
1	9	36	84	126	126	84	36	9	1
#include <iostream>

using namespace std;
/*
C++ program
Print given row in pascal triangle
*/
class PascalTriangle
{
public:
// Print the elements of given nth row in pascal triangle
void pascal_triangle_row(int n)
{
if (n <= 0)
{
return;
}
cout << "\n Row " << n << " \n ";
//Loop controlling variables
int i = n, j = 0, k = 0;
//print row
for (j = 0; j <= n; j++)
{
if (j == 0 || i == 0)
{
//Set initial values
k = 1;
}
else
{
//Pascal's triangle number calculation
k = k *(i - j + 1) / j;
}
//Display the value of calculate number
cout << "" << k << "\t";
}
cout << "\n";
}
};
int main()
{
PascalTriangle obj = PascalTriangle();
//Test Cases
obj.pascal_triangle_row(5);
obj.pascal_triangle_row(1);
obj.pascal_triangle_row(7);
obj.pascal_triangle_row(9);
return 0;
}

Output

Row 5
1	5	10	10	5	1

Row 1
1	1

Row 7
1	7	21	35	35	21	7	1

Row 9
1	9	36	84	126	126	84	36	9	1
//Include namespace system
using System;
/*
C# program
Print given row in pascal triangle
*/
class PascalTriangle
{
// Print the elements of given nth row in pascal triangle
public void pascal_triangle_row(int n)
{
if (n <= 0)
{
return;
}
Console.Write("\n Row " + n + " \n ");
//Loop controlling variables
int i = n, j = 0, k = 0;
//print row
for (j = 0; j <= n; j++)
{
if (j == 0 || i == 0)
{
//Set initial values
k = 1;
}
else
{
//Pascal's triangle number calculation
k = k * (i - j + 1) / j;
}
//Display the value of calculate number
Console.Write("" + k + "\t");
}
Console.Write("\n");
}
public static void Main(String[] args)
{
PascalTriangle obj = new PascalTriangle();
//Test Cases
obj.pascal_triangle_row(5);
obj.pascal_triangle_row(1);
obj.pascal_triangle_row(7);
obj.pascal_triangle_row(9);
}
}

Output

Row 5
1	5	10	10	5	1

Row 1
1	1

Row 7
1	7	21	35	35	21	7	1

Row 9
1	9	36	84	126	126	84	36	9	1
<?php
/*
Php program
Print given row in pascal triangle
*/
class PascalTriangle
{
// Print the elements of given nth row in pascal triangle
public	function pascal_triangle_row(\$n)
{
if (\$n <= 0)
{
return;
}
echo "\n Row ". \$n ." \n ";
//Loop controlling variables
\$i = \$n;
\$j = 0;
\$k = 0;
//print row
for (\$j = 0; \$j <= \$n; \$j++)
{
if (\$j == 0 || \$i == 0)
{
//Set initial values
\$k = 1;
}
else
{
//Pascal's triangle number calculation
\$k = intval(\$k * (\$i - \$j + 1) / \$j);
}
//Display the value of calculate number
echo "". \$k ."\t";
}
echo "\n";
}
}

function main()
{
\$obj = new PascalTriangle();
//Test Cases
\$obj->pascal_triangle_row(5);
\$obj->pascal_triangle_row(1);
\$obj->pascal_triangle_row(7);
\$obj->pascal_triangle_row(9);
}
main();

Output

Row 5
1	5	10	10	5	1

Row 1
1	1

Row 7
1	7	21	35	35	21	7	1

Row 9
1	9	36	84	126	126	84	36	9	1
/*
Node Js program
Print given row in pascal triangle
*/
class PascalTriangle
{
// Print the elements of given nth row in pascal triangle
pascal_triangle_row(n)
{
if (n <= 0)
{
return;
}
process.stdout.write("\n Row " + n + " \n ");
//Loop controlling variables
var i = n;
var j = 0;
var k = 0;
//print row
for (j = 0; j <= n; j++)
{
if (j == 0 || i == 0)
{
//Set initial values
k = 1;
}
else
{
//Pascal's triangle number calculation
k = parseInt(k * (i - j + 1) / j);
}
//Display the value of calculate number
process.stdout.write("" + k + "\t");
}
process.stdout.write("\n");
}
}

function main()
{
var obj = new PascalTriangle();
//Test Cases
obj.pascal_triangle_row(5);
obj.pascal_triangle_row(1);
obj.pascal_triangle_row(7);
obj.pascal_triangle_row(9);
}
main();

Output

Row 5
1	5	10	10	5	1

Row 1
1	1

Row 7
1	7	21	35	35	21	7	1

Row 9
1	9	36	84	126	126	84	36	9	1
#   Python 3 program
#   Print given row in pascal triangle

class PascalTriangle :
#  Print the elements of given nth row in pascal triangle
def pascal_triangle_row(self, n) :
if (n <= 0) :
return

print("\n Row ", n ," \n ", end = "")
# Loop controlling variables
i = n
j = 0
k = 0
# print row
while (j <= n) :
if (j == 0 or i == 0) :
# Set initial values
k = 1
else :
# Pascal's triangle number calculation
k = int(k * (i - j + 1) / j)

# Display the value of calculate number
print("", k ,"\t", end = "")
j += 1

print("\n", end = "")

def main() :
obj = PascalTriangle()
# Test Cases
obj.pascal_triangle_row(5)
obj.pascal_triangle_row(1)
obj.pascal_triangle_row(7)
obj.pascal_triangle_row(9)

if __name__ == "__main__": main()

Output

Row  5
1 	 5 	 10 	 10 	 5 	 1

Row  1
1 	 1

Row  7
1 	 7 	 21 	 35 	 35 	 21 	 7 	 1

Row  9
1 	 9 	 36 	 84 	 126 	 126 	 84 	 36 	 9 	 1
/*
Scala program
Print given row in pascal triangle
*/
class PascalTriangle
{
// Print the elements of given nth row in pascal triangle
def pascal_triangle_row(n: Int): Unit = {
if (n <= 0)
{
return;
}
print("\n Row " + n + " \n ");
//Loop controlling variables
var i: Int = n;
var j: Int = 0;
var k: Int = 0;
//print row
while (j <= n)
{
if (j == 0 || i == 0)
{
//Set initial values
k = 1;
}
else
{
//Pascal's triangle number calculation
k = (k * (i - j + 1) / j).toInt;
}
//Display the value of calculate number
print("" + k + "\t");
j += 1;
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: PascalTriangle = new PascalTriangle();
//Test Cases
obj.pascal_triangle_row(5);
obj.pascal_triangle_row(1);
obj.pascal_triangle_row(7);
obj.pascal_triangle_row(9);
}
}

Output

Row 5
1	5	10	10	5	1

Row 1
1	1

Row 7
1	7	21	35	35	21	7	1

Row 9
1	9	36	84	126	126	84	36	9	1
/*
Swift 4 program
Print given row in pascal triangle
*/
class PascalTriangle
{
// Print the elements of given nth row in pascal triangle
func pascal_triangle_row(_ n: Int)
{
if (n <= 0)
{
return;
}
print("\n Row ", n ," \n ", terminator: "");
//Loop controlling variables
let i: Int = n;
var j: Int = 0;
var k: Int = 0;
//print row
while (j <= n)
{
if (j == 0 || i == 0)
{
//Set initial values
k = 1;
}
else
{
//Pascal"s triangle number calculation
k = k * (i - j + 1) / j;
}
//Display the value of calculate number
print("", k ,"\t", terminator: "");
j += 1;
}
print("\n", terminator: "");
}
}
func main()
{
let obj: PascalTriangle = PascalTriangle();
//Test Cases
obj.pascal_triangle_row(5);
obj.pascal_triangle_row(1);
obj.pascal_triangle_row(7);
obj.pascal_triangle_row(9);
}
main();

Output

Row  5
1 	 5 	 10 	 10 	 5 	 1

Row  1
1 	 1

Row  7
1 	 7 	 21 	 35 	 35 	 21 	 7 	 1

Row  9
1 	 9 	 36 	 84 	 126 	 126 	 84 	 36 	 9 	 1
fn main()
{
//Test Cases
pascal_triangle_row(5);
pascal_triangle_row(1);
pascal_triangle_row(7);
pascal_triangle_row(9);
}
fn pascal_triangle_row(n: i32)
{
if n <= 0
{
return;
}
print!("\n Row {} \n ", n);
//Loop controlling variables
let i: i32 = n;
let mut j: i32 = 0;
let mut k: i32 = 0;
while  j <= n
{
if j == 0 || i == 0
{
//Set initial values
k = 1;
}
else
{
//Pascal's triangle number calculation
k = k * (i - j + 1) / j;
}
//Display the value of calculate number
print!("{}\t", k);
j += 1
}
print!("\n");
}

Output

Row 5
1	5	10	10	5	1

Row 1
1	1

Row 7
1	7	21	35	35	21	7	1

Row 9
1	9	36	84	126	126	84	36	9	1
#   Ruby program
#   Print given row in pascal triangle

class PascalTriangle
#  Print the elements of given nth row in pascal triangle
def pascal_triangle_row(n)
if (n <= 0)
return
end

print("\n Row ", n ," \n ")
# Loop controlling variables
i = n
j = 0
k = 0
# print row
while (j <= n)
if (j == 0 || i == 0)
# Set initial values
k = 1
else
# Pascal's triangle number calculation
k = k * (i - j + 1) / j
end

# Display the value of calculate number
print( k ,"\t")
j+=1
end
print("\n")
end

end

def main()
obj = PascalTriangle.new()
# Test Cases
obj.pascal_triangle_row(5)
obj.pascal_triangle_row(1)
obj.pascal_triangle_row(7)
obj.pascal_triangle_row(9)
end

main()

Output

Row 5
1	5	10	10	5	1

Row 1
1	1

Row 7
1	7	21	35	35	21	7	1

Row 9
1	9	36	84	126	126	84	36	9	1

Time Complexity

The time complexity of this code is O(n), where n is the index of the row for which we want to print the elements. This is because the code iterates through the row's elements using a single loop, and the calculation of each element takes constant time.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.