Print given row in pascal triangle

Here given code implementation process.

/*
  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 header file
#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	

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.

New Comment







© 2021, kalkicode.com, All rights reserved