Posted on by Kalkicode
Code Mathematics

Find the sum of nth row in pascal's triangle

The sum of the numbers in the nth row of Pascal's triangle is equal to 2^n.

Pascal's triangle is a triangular array of numbers where each number in the triangle is the sum of the two numbers directly above it. The first row contains only the number 1, and each subsequent row is constructed by adding a 1 at the beginning and end of the previous row.

For example, the first few rows of Pascal's triangle look like this:

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

To find the sum of the numbers in the nth row, you simply add up all the numbers in that row. For example, the sum of the numbers in the 5th row is:

1 + 4 + 6 + 4 + 1 = 16

The formula for the sum of the numbers in the nth row is 2^n. So, in this case, 2^5 = 32, which is twice the sum of the numbers in the 5th row. This formula holds true for all rows of Pascal's triangle.

/*
    C program for
    Find the sum of nth row in pascal's triangle
*/
#include <stdio.h>

// Sum of given row in pascal triangle
void sumPascalRow(int n)
{
	if (n <= 0)
	{
		return;
	}
	printf("\n Row %d \n", n);
	// Assume number is not overflow
	long long int sum = (1 << n);
	printf(" Sum : %lld \n", sum);
}
int main(int argc, char
	const *argv[])
{
	/*

	              1                 0-th row
	            1   1               1-st row
	          1   2   1             2-nd row
	        1   3   3   1           3-th row
	      1   4   6   4   1         4-th row
	    1   5   10   10   5   1     5-th row
	  1   6   15   20   15   6   1  6-th row
	-----------------------------------------
	        Pascal's triangle
	*/
	/*
	    Example A

	    Row = 5th
	    [ 1  5   10  10  5   1 ]
	    -------------------------
	    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
	*/
	sumPascalRow(5);
	/*
	    Example B

	    Row = 2nd
	    [ 1   2   1 ]
	    -------------------------
	    [ 1 + 2 + 1 ] = 4
	*/
	sumPascalRow(2);
	/*
	    Example C

	    Row = 7th
	    [ 1  7  21  35  35  21  7  1]
	    ------------------------------------------
	    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
	*/
	sumPascalRow(7);
	return 0;
}

Output

 Row 5
 Sum : 32

 Row 2
 Sum : 4

 Row 7
 Sum : 128
// Java program for
// Find the sum of nth row in pascal's triangle
public class PascalTriangle
{
	// Sum of given row in pascal triangle
	public void sumPascalRow(int n)
	{
		if (n <= 0)
		{
			return;
		}
		System.out.print("\n Row " + n + " \n");
		// Assume number is not overflow
		long sum = (1 << n);
		System.out.print(" Sum : " + sum + " \n");
	}
	public static void main(String[] args)
	{
		PascalTriangle task = new PascalTriangle();
		/*

		               1                 0-th row
		             1   1               1-st row
		           1   2   1             2-nd row
		         1   3   3   1           3-th row
		       1   4   6   4   1         4-th row
		     1   5   10   10   5   1     5-th row
		  1   6   15   20   15   6   1   6-th row
		  ---------------------------------------
		        Pascal's triangle
		*/
		/*
		    Example A

		    Row = 5th
		    [ 1  5   10  10  5   1 ]
		    -------------------------
		    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
		*/
		task.sumPascalRow(5);
		/*
		    Example B

		    Row = 2nd
		    [ 1   2   1 ]
		    -------------------------
		    [ 1 + 2 + 1 ] = 4
		*/
		task.sumPascalRow(2);
		/*
		    Example C

		    Row = 7th
		    [ 1  7  21  35  35  21  7  1]
		    ------------------------------------------
		    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
		*/
		task.sumPascalRow(7);
	}
}

Output

 Row 5
 Sum : 32

 Row 2
 Sum : 4

 Row 7
 Sum : 128
// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Find the sum of nth row in pascal's triangle
class PascalTriangle
{
	public:
		// Sum of given row in pascal triangle
		void sumPascalRow(int n)
		{
			if (n <= 0)
			{
				return;
			}
			cout << "\n Row " << n << " \n";
			// Assume number is not overflow
			long sum = (1 << n);
			cout << " Sum : " << sum << " \n";
		}
};
int main()
{
	PascalTriangle *task = new PascalTriangle();
	/*
	               1                 0-th row
	             1   1               1-st row
	           1   2   1             2-nd row
	         1   3   3   1           3-th row
	       1   4   6   4   1         4-th row
	     1   5   10   10   5   1     5-th row
	  1   6   15   20   15   6   1   6-th row
	  ---------------------------------------
	        Pascal's triangle
	*/
	/*
	    Example A
	    Row = 5th
	    [ 1  5   10  10  5   1 ]
	    -------------------------
	    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
	*/
	task->sumPascalRow(5);
	/*
	    Example B
	    Row = 2nd
	    [ 1   2   1 ]
	    -------------------------
	    [ 1 + 2 + 1 ] = 4
	*/
	task->sumPascalRow(2);
	/*
	    Example C
	    Row = 7th
	    [ 1  7  21  35  35  21  7  1]
	    ------------------------------------------
	    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
	*/
	task->sumPascalRow(7);
	return 0;
}

Output

 Row 5
 Sum : 32

 Row 2
 Sum : 4

 Row 7
 Sum : 128
// Include namespace system
using System;
// Csharp program for
// Find the sum of nth row in pascal's triangle
public class PascalTriangle
{
	// Sum of given row in pascal triangle
	public void sumPascalRow(int n)
	{
		if (n <= 0)
		{
			return;
		}
		Console.Write("\n Row " + n + " \n");
		// Assume number is not overflow
		long sum = (1 << n);
		Console.Write(" Sum : " + sum + " \n");
	}
	public static void Main(String[] args)
	{
		PascalTriangle task = new PascalTriangle();
		/*
		               1                 0-th row
		             1   1               1-st row
		           1   2   1             2-nd row
		         1   3   3   1           3-th row
		       1   4   6   4   1         4-th row
		     1   5   10   10   5   1     5-th row
		  1   6   15   20   15   6   1   6-th row
		  ---------------------------------------
		        Pascal's triangle
		*/
		/*
		    Example A
		    Row = 5th
		    [ 1  5   10  10  5   1 ]
		    -------------------------
		    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
		*/
		task.sumPascalRow(5);
		/*
		    Example B
		    Row = 2nd
		    [ 1   2   1 ]
		    -------------------------
		    [ 1 + 2 + 1 ] = 4
		*/
		task.sumPascalRow(2);
		/*
		    Example C
		    Row = 7th
		    [ 1  7  21  35  35  21  7  1]
		    ------------------------------------------
		    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
		*/
		task.sumPascalRow(7);
	}
}

Output

 Row 5
 Sum : 32

 Row 2
 Sum : 4

 Row 7
 Sum : 128
package main
import "fmt"
// Go program for
// Find the sum of nth row in pascal's triangle
type PascalTriangle struct {}
func getPascalTriangle() * PascalTriangle {
	var me *PascalTriangle = &PascalTriangle {}
	return me
}
// Sum of given row in pascal triangle
func(this PascalTriangle) sumPascalRow(n int) {
	if n <= 0 {
		return
	}
	fmt.Print("\n Row ", n, " \n")
	// Assume number is not overflow
	var sum int64 = (1 << n)
	fmt.Print(" Sum : ", sum, " \n")
}
func main() {
	var task * PascalTriangle = getPascalTriangle()
	/*
	               1                 0-th row
	             1   1               1-st row
	           1   2   1             2-nd row
	         1   3   3   1           3-th row
	       1   4   6   4   1         4-th row
	     1   5   10   10   5   1     5-th row
	  1   6   15   20   15   6   1   6-th row
	  ---------------------------------------
	        Pascal's triangle
	*/
	/*
	    Example A
	    Row = 5th
	    [ 1  5   10  10  5   1 ]
	    -------------------------
	    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
	*/
	task.sumPascalRow(5)
	/*
	    Example B
	    Row = 2nd
	    [ 1   2   1 ]
	    -------------------------
	    [ 1 + 2 + 1 ] = 4
	*/
	task.sumPascalRow(2)
	/*
	    Example C
	    Row = 7th
	    [ 1  7  21  35  35  21  7  1]
	    ------------------------------------------
	    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
	*/
	task.sumPascalRow(7)
}

Output

 Row 5
 Sum : 32

 Row 2
 Sum : 4

 Row 7
 Sum : 128
<?php
// Php program for
// Find the sum of nth row in pascal's triangle
class PascalTriangle
{
	// Sum of given row in pascal triangle
	public	function sumPascalRow($n)
	{
		if ($n <= 0)
		{
			return;
		}
		echo("\n Row ".$n." \n");
		// Assume number is not overflow
		$sum = (1 << $n);
		echo(" Sum : ".$sum." \n");
	}
}

function main()
{
	$task = new PascalTriangle();
	/*
	               1                 0-th row
	             1   1               1-st row
	           1   2   1             2-nd row
	         1   3   3   1           3-th row
	       1   4   6   4   1         4-th row
	     1   5   10   10   5   1     5-th row
	  1   6   15   20   15   6   1   6-th row
	  ---------------------------------------
	        Pascal's triangle
	*/
	/*
	    Example A
	    Row = 5th
	    [ 1  5   10  10  5   1 ]
	    -------------------------
	    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
	*/
	$task->sumPascalRow(5);
	/*
	    Example B
	    Row = 2nd
	    [ 1   2   1 ]
	    -------------------------
	    [ 1 + 2 + 1 ] = 4
	*/
	$task->sumPascalRow(2);
	/*
	    Example C
	    Row = 7th
	    [ 1  7  21  35  35  21  7  1]
	    ------------------------------------------
	    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
	*/
	$task->sumPascalRow(7);
}
main();

Output

 Row 5
 Sum : 32

 Row 2
 Sum : 4

 Row 7
 Sum : 128
// Node JS program for
// Find the sum of nth row in pascal's triangle
class PascalTriangle
{
	// Sum of given row in pascal triangle
	sumPascalRow(n)
	{
		if (n <= 0)
		{
			return;
		}
		process.stdout.write("\n Row " + n + " \n");
		// Assume number is not overflow
		var sum = (1 << n);
		process.stdout.write(" Sum : " + sum + " \n");
	}
}

function main()
{
	var task = new PascalTriangle();
	/*
	               1                 0-th row
	             1   1               1-st row
	           1   2   1             2-nd row
	         1   3   3   1           3-th row
	       1   4   6   4   1         4-th row
	     1   5   10   10   5   1     5-th row
	  1   6   15   20   15   6   1   6-th row
	  ---------------------------------------
	        Pascal's triangle
	*/
	/*
	    Example A
	    Row = 5th
	    [ 1  5   10  10  5   1 ]
	    -------------------------
	    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
	*/
	task.sumPascalRow(5);
	/*
	    Example B
	    Row = 2nd
	    [ 1   2   1 ]
	    -------------------------
	    [ 1 + 2 + 1 ] = 4
	*/
	task.sumPascalRow(2);
	/*
	    Example C
	    Row = 7th
	    [ 1  7  21  35  35  21  7  1]
	    ------------------------------------------
	    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
	*/
	task.sumPascalRow(7);
}
main();

Output

 Row 5
 Sum : 32

 Row 2
 Sum : 4

 Row 7
 Sum : 128
#  Python 3 program for
#  Find the sum of nth row in pascal's triangle
class PascalTriangle :
	#  Sum of given row in pascal triangle
	def sumPascalRow(self, n) :
		if (n <= 0) :
			return
		
		print("\n Row ", n ," ")
		#  Assume number is not overflow
		sum = (1 << n)
		print(" Sum : ", sum ," ")
	

def main() :
	task = PascalTriangle()
	#               1                 0-th row
	#             1   1               1-st row
	#           1   2   1             2-nd row
	#         1   3   3   1           3-th row
	#       1   4   6   4   1         4-th row
	#     1   5   10   10   5   1     5-th row
	#  1   6   15   20   15   6   1   6-th row
	#  ---------------------------------------
	#        Pascal's triangle
	#    Example A
	#    Row = 5th
	#    [ 1  5   10  10  5   1 ]
	#    -------------------------
	#    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
	task.sumPascalRow(5)
	#    Example B
	#    Row = 2nd
	#    [ 1   2   1 ]
	#    -------------------------
	#    [ 1 + 2 + 1 ] = 4
	task.sumPascalRow(2)
	#    Example C
	#    Row = 7th
	#    [ 1  7  21  35  35  21  7  1]
	#    ------------------------------------------
	#    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
	task.sumPascalRow(7)

if __name__ == "__main__": main()

Output

 Row  5
 Sum :  32

 Row  2
 Sum :  4

 Row  7
 Sum :  128
#  Ruby program for
#  Find the sum of nth row in pascal's triangle
class PascalTriangle 
	#  Sum of given row in pascal triangle
	def sumPascalRow(n) 
		if (n <= 0) 
			return
		end

		print("\n Row ", n ," \n")
		#  Assume number is not overflow
		sum = (1 << n)
		print(" Sum : ", sum ," \n")
	end

end

def main() 
	task = PascalTriangle.new()
	#               1                 0-th row
	#             1   1               1-st row
	#           1   2   1             2-nd row
	#         1   3   3   1           3-th row
	#       1   4   6   4   1         4-th row
	#     1   5   10   10   5   1     5-th row
	#  1   6   15   20   15   6   1   6-th row
	#  ---------------------------------------
	#        Pascal's triangle
	#    Example A
	#    Row = 5th
	#    [ 1  5   10  10  5   1 ]
	#    -------------------------
	#    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
	task.sumPascalRow(5)
	#    Example B
	#    Row = 2nd
	#    [ 1   2   1 ]
	#    -------------------------
	#    [ 1 + 2 + 1 ] = 4
	task.sumPascalRow(2)
	#    Example C
	#    Row = 7th
	#    [ 1  7  21  35  35  21  7  1]
	#    ------------------------------------------
	#    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
	task.sumPascalRow(7)
end

main()

Output

 Row 5 
 Sum : 32 

 Row 2 
 Sum : 4 

 Row 7 
 Sum : 128 
// Scala program for
// Find the sum of nth row in pascal's triangle
class PascalTriangle()
{
	// Sum of given row in pascal triangle
	def sumPascalRow(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		print("\n Row " + n + " \n");
		// Assume number is not overflow
		var sum: Long = (1 << n);
		print(" Sum : " + sum + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PascalTriangle = new PascalTriangle();
		/*
		               1                 0-th row
		             1   1               1-st row
		           1   2   1             2-nd row
		         1   3   3   1           3-th row
		       1   4   6   4   1         4-th row
		     1   5   10   10   5   1     5-th row
		  1   6   15   20   15   6   1   6-th row
		  ---------------------------------------
		        Pascal's triangle
		*/
		/*
		    Example A
		    Row = 5th
		    [ 1  5   10  10  5   1 ]
		    -------------------------
		    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
		*/
		task.sumPascalRow(5);
		/*
		    Example B
		    Row = 2nd
		    [ 1   2   1 ]
		    -------------------------
		    [ 1 + 2 + 1 ] = 4
		*/
		task.sumPascalRow(2);
		/*
		    Example C
		    Row = 7th
		    [ 1  7  21  35  35  21  7  1]
		    ------------------------------------------
		    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
		*/
		task.sumPascalRow(7);
	}
}

Output

 Row 5
 Sum : 32

 Row 2
 Sum : 4

 Row 7
 Sum : 128
// Swift 4 program for
// Find the sum of nth row in pascal's triangle
class PascalTriangle
{
	// Sum of given row in pascal triangle
	func sumPascalRow(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		print("\n Row ", n ," ");
		// Assume number is not overflow
		let sum: Int = (1 << n);
		print(" Sum : ", sum ," ");
	}
}
func main()
{
	let task: PascalTriangle = PascalTriangle();
	/*
	               1                 0-th row
	             1   1               1-st row
	           1   2   1             2-nd row
	         1   3   3   1           3-th row
	       1   4   6   4   1         4-th row
	     1   5   10   10   5   1     5-th row
	  1   6   15   20   15   6   1   6-th row
	  ---------------------------------------
	        Pascal's triangle
	*/
	/*
	    Example A
	    Row = 5th
	    [ 1  5   10  10  5   1 ]
	    -------------------------
	    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
	*/
	task.sumPascalRow(5);
	/*
	    Example B
	    Row = 2nd
	    [ 1   2   1 ]
	    -------------------------
	    [ 1 + 2 + 1 ] = 4
	*/
	task.sumPascalRow(2);
	/*
	    Example C
	    Row = 7th
	    [ 1  7  21  35  35  21  7  1]
	    ------------------------------------------
	    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
	*/
	task.sumPascalRow(7);
}
main();

Output

 Row  5
 Sum :  32

 Row  2
 Sum :  4

 Row  7
 Sum :  128
// Kotlin program for
// Find the sum of nth row in pascal's triangle
class PascalTriangle
{
	// Sum of given row in pascal triangle
	fun sumPascalRow(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		print("\n Row " + n + " \n");
		// Assume number is not overflow
		val sum = (1 shl n);
		print(" Sum : " + sum + " \n");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: PascalTriangle = PascalTriangle();
	/*
	               1                 0-th row
	             1   1               1-st row
	           1   2   1             2-nd row
	         1   3   3   1           3-th row
	       1   4   6   4   1         4-th row
	     1   5   10   10   5   1     5-th row
	  1   6   15   20   15   6   1   6-th row
	  ---------------------------------------
	        Pascal's triangle
	*/
	/*
	    Example A
	    Row = 5th
	    [ 1  5   10  10  5   1 ]
	    -------------------------
	    [ 1 + 5 + 10 + 10 + 5 + 1 ] = 32
	*/
	task.sumPascalRow(5);
	/*
	    Example B
	    Row = 2nd
	    [ 1   2   1 ]
	    -------------------------
	    [ 1 + 2 + 1 ] = 4
	*/
	task.sumPascalRow(2);
	/*
	    Example C
	    Row = 7th
	    [ 1  7  21  35  35  21  7  1]
	    ------------------------------------------
	    [ 1 + 7 + 21 + 35 + 35 + 21 + 7 + 1] = 128
	*/
	task.sumPascalRow(7);
}

Output

 Row 5
 Sum : 32

 Row 2
 Sum : 4

 Row 7
 Sum : 128

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.

New Comment