Sum triangle of array using recursion

Here given code implementation process.

// C program for 
// Sum triangle of array using recursion 
#include <stdio.h>

// Print the sum triangle by recursion
void sumTriangle(int arr[], int n)
{
	if (n < 1)
	{
		// Stop recursion
		return;
	}
	else
	{
		// Define auxiliary array
		int auxiliary[n - 1];
		int sum = 0;
		// Execute the loop through by n
		for (int i = 0; i < n - 1; i++)
		{
			// Calculate sum the elements of two consecutive elements
			sum = arr[i] + arr[i + 1];
			// Put sum
			auxiliary[i] = sum;
		}
		// Reduce size and pass new auxiliary array to 
		sumTriangle(auxiliary, n - 1);
		// Display calculated result
		for (int i = 0; i < n; i++)
		{
			printf("%d  ", arr[i]);
		}
		printf("\n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Given array elements
	int arr[] = {
		1 , 4 , 3 , 1 , 6 , 5
	};
	/*
	---------------------------------
	               96  
	            45    51  
	         23    22    29  
	      12    11    11    18  
	   5     7     4     7     11  
	1     4     3     1     6      5   
	---------------------------------
	*/
	// Get the size
	int n = sizeof(arr) / sizeof(arr[0]);
	// Test
	sumTriangle(arr, n);
	return 0;
}

input

96
45  51
23  22  29
12  11  11  18
5  7  4  7  11
1  4  3  1  6  5
/*
  Java Program for
  Sum triangle of array using recursion 
*/
public class Triangle
{
	// Print the sum triangle by recursion
	public void sumTriangle(int[] arr, int n)
	{
		if (n < 1)
		{
			// Stop recursion
			return;
		}
		else
		{
			// Define auxiliary array
			int[] auxiliary = new int[n - 1];
			int sum = 0;
			// Execute the loop through by n
			for (int i = 0; i < n - 1; i++)
			{
				// Calculate sum the elements of two consecutive elements
				sum = arr[i] + arr[i + 1];
				// Put sum
				auxiliary[i] = sum;
			}
			// Reduce size and pass new auxiliary array to 
			sumTriangle(auxiliary, n - 1);
			// Display calculated result
			for (int i = 0; i < n; i++)
			{
				System.out.print("  " + arr[i]);
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		Triangle task = new Triangle();
		// Given array elements
		int[] arr = {
			1 , 4 , 3 , 1 , 6 , 5
		};
		/*
		---------------------------------
		               96  
		            45    51  
		         23    22    29  
		      12    11    11    18  
		   5     7     4     7     11  
		1     4     3     1     6      5   
		---------------------------------
		*/
		// Get the size
		int n = arr.length;
		// Test
		task.sumTriangle(arr, n);
	}
}

input

  96
  45  51
  23  22  29
  12  11  11  18
  5  7  4  7  11
  1  4  3  1  6  5
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Sum triangle of array using recursion 
*/
class Triangle
{
	public:
		// Print the sum triangle by recursion
		void sumTriangle(int arr[], int n)
		{
			if (n < 1)
			{
				// Stop recursion
				return;
			}
			else
			{
				// Define auxiliary array
				int auxiliary[n - 1];
				int sum = 0;
				// Execute the loop through by n
				for (int i = 0; i < n - 1; i++)
				{
					// Calculate sum the elements of two consecutive elements
					sum = arr[i] + arr[i + 1];
					// Put sum
					auxiliary[i] = sum;
				}
				// Reduce size and pass new auxiliary array to
				this->sumTriangle(auxiliary, n - 1);
				// Display calculated result
				for (int i = 0; i < n; i++)
				{
					cout << "  " << arr[i];
				}
				cout << "\n";
			}
		}
};
int main()
{
	Triangle *task = new Triangle();
	// Given array elements
	int arr[] = {
		1 , 4 , 3 , 1 , 6 , 5
	};
	/*
	---------------------------------
	               96  
	            45    51  
	         23    22    29  
	      12    11    11    18  
	   5     7     4     7     11  
	1     4     3     1     6      5   
	---------------------------------
	*/
	// Get the size
	int n = sizeof(arr) / sizeof(arr[0]);
	// Test
	task->sumTriangle(arr, n);
	return 0;
}

input

  96
  45  51
  23  22  29
  12  11  11  18
  5  7  4  7  11
  1  4  3  1  6  5
// Include namespace system
using System;
/*
  Csharp Program for
  Sum triangle of array using recursion 
*/
public class Triangle
{
	// Print the sum triangle by recursion
	public void sumTriangle(int[] arr, int n)
	{
		if (n < 1)
		{
			// Stop recursion
			return;
		}
		else
		{
			// Define auxiliary array
			int[] auxiliary = new int[n - 1];
			int sum = 0;
			// Execute the loop through by n
			for (int i = 0; i < n - 1; i++)
			{
				// Calculate sum the elements of two consecutive elements
				sum = arr[i] + arr[i + 1];
				// Put sum
				auxiliary[i] = sum;
			}
			// Reduce size and pass new auxiliary array to
			this.sumTriangle(auxiliary, n - 1);
			// Display calculated result
			for (int i = 0; i < n; i++)
			{
				Console.Write("  " + arr[i]);
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		Triangle task = new Triangle();
		// Given array elements
		int[] arr = {
			1 , 4 , 3 , 1 , 6 , 5
		};
		/*
		---------------------------------
		               96  
		            45    51  
		         23    22    29  
		      12    11    11    18  
		   5     7     4     7     11  
		1     4     3     1     6      5   
		---------------------------------
		*/
		// Get the size
		int n = arr.Length;
		// Test
		task.sumTriangle(arr, n);
	}
}

input

  96
  45  51
  23  22  29
  12  11  11  18
  5  7  4  7  11
  1  4  3  1  6  5
<?php
/*
  Php Program for
  Sum triangle of array using recursion 
*/
class Triangle
{
	// Print the sum triangle by recursion
	public	function sumTriangle($arr, $n)
	{
		if ($n < 1)
		{
			// Stop recursion
			return;
		}
		else
		{
			// Define auxiliary array
			$auxiliary = array_fill(0, $n - 1, 0);
			$sum = 0;
			// Execute the loop through by n
			for ($i = 0; $i < $n - 1; $i++)
			{
				// Calculate sum the elements of two consecutive elements
				$sum = $arr[$i] + $arr[$i + 1];
				// Put sum
				$auxiliary[$i] = $sum;
			}
			// Reduce size and pass new auxiliary array to
			$this->sumTriangle($auxiliary, $n - 1);
			// Display calculated result
			for ($i = 0; $i < $n; $i++)
			{
				echo "  ".$arr[$i];
			}
			echo "\n";
		}
	}
}

function main()
{
	$task = new Triangle();
	// Given array elements
	$arr = array(1, 4, 3, 1, 6, 5);
	/*
	---------------------------------
	               96  
	            45    51  
	         23    22    29  
	      12    11    11    18  
	   5     7     4     7     11  
	1     4     3     1     6      5   
	---------------------------------
	*/
	// Get the size
	$n = count($arr);
	// Test
	$task->sumTriangle($arr, $n);
}
main();

input

  96
  45  51
  23  22  29
  12  11  11  18
  5  7  4  7  11
  1  4  3  1  6  5
/*
  Node JS Program for
  Sum triangle of array using recursion 
*/
class Triangle
{
	// Print the sum triangle by recursion
	sumTriangle(arr, n)
	{
		if (n < 1)
		{
			// Stop recursion
			return;
		}
		else
		{
			// Define auxiliary array
			var auxiliary = Array(n - 1).fill(0);
			var sum = 0;
			// Execute the loop through by n
			for (var i = 0; i < n - 1; i++)
			{
				// Calculate sum the elements of two consecutive elements
				sum = arr[i] + arr[i + 1];
				// Put sum
				auxiliary[i] = sum;
			}
			// Reduce size and pass new auxiliary array to
			this.sumTriangle(auxiliary, n - 1);
			// Display calculated result
			for (var i = 0; i < n; i++)
			{
				process.stdout.write("  " + arr[i]);
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var task = new Triangle();
	// Given array elements
	var arr = [1, 4, 3, 1, 6, 5];
	/*
	---------------------------------
	               96  
	            45    51  
	         23    22    29  
	      12    11    11    18  
	   5     7     4     7     11  
	1     4     3     1     6      5   
	---------------------------------
	*/
	// Get the size
	var n = arr.length;
	// Test
	task.sumTriangle(arr, n);
}
main();

input

  96
  45  51
  23  22  29
  12  11  11  18
  5  7  4  7  11
  1  4  3  1  6  5
#  Python 3 Program for
#  Sum triangle of array using recursion 

class Triangle :
	#  Print the sum triangle by recursion
	def sumTriangle(self, arr, n) :
		if (n < 1) :
			#  Stop recursion
			return
		else :
			auxiliary = [0] * (n - 1)
			sum = 0
			i = 0
			#  Execute the loop through by n
			while (i < n - 1) :
				#  Calculate sum the elements of two consecutive elements
				sum = arr[i] + arr[i + 1]
				#  Put sum
				auxiliary[i] = sum
				i += 1
			
			#  Reduce size and pass new auxiliary list to
			self.sumTriangle(auxiliary, n - 1)
			i = 0
			#  Display calculated result
			while (i < n) :
				print("  ", arr[i], end = "")
				i += 1
			
			print(end = "\n")
		
	

def main() :
	task = Triangle()
	arr = [1, 4, 3, 1, 6, 5]
	n = len(arr)
	#  Test
	task.sumTriangle(arr, n)

if __name__ == "__main__": main()

input

   96
   45   51
   23   22   29
   12   11   11   18
   5   7   4   7   11
   1   4   3   1   6   5
#  Ruby Program for
#  Sum triangle of array using recursion 

class Triangle 
	#  Print the sum triangle by recursion
	def sumTriangle(arr, n) 
		if (n < 1) 
			#  Stop recursion
			return
		else 
			#  Define auxiliary array
			auxiliary = Array.new(n - 1) {0}
			sum = 0
			i = 0
			#  Execute the loop through by n
			while (i < n - 1) 
				#  Calculate sum the elements of two consecutive elements
				sum = arr[i] + arr[i + 1]
				#  Put sum
				auxiliary[i] = sum
				i += 1
			end

			#  Reduce size and pass new auxiliary array to
			self.sumTriangle(auxiliary, n - 1)
			i = 0
			#  Display calculated result
			while (i < n) 
				print("  ", arr[i])
				i += 1
			end

			print("\n")
		end

	end

end

def main() 
	task = Triangle.new()
	#  Given array elements
	arr = [1, 4, 3, 1, 6, 5]
	# ---------------------------------
	#               96  
	#            45    51  
	#         23    22    29  
	#      12    11    11    18  
	#   5     7     4     7     11  
	# 1     4     3     1     6      5   
	# ---------------------------------
	#  Get the size
	n = arr.length
	#  Test
	task.sumTriangle(arr, n)
end

main()

input

  96
  45  51
  23  22  29
  12  11  11  18
  5  7  4  7  11
  1  4  3  1  6  5
/*
  Swift 4 Program for
  Sum triangle of array using recursion 
*/
class Triangle
{
	// Print the sum triangle by recursion
	func sumTriangle(_ arr: inout[Int], _ n: Int)
	{
		if (n < 1)
		{
			// Stop recursion
			return;
		}
		else
		{
			// Define auxiliary array
			var auxiliary: [Int] = Array(repeating: 0, count: n - 1);
			var sum: Int = 0;
			var i: Int = 0;
			// Execute the loop through by n
			while (i < n - 1)
			{
				// Calculate sum the elements of two consecutive elements
				sum = arr[i] + arr[i + 1];
				// Put sum
				auxiliary[i] = sum;
				i += 1;
			}
			// Reduce size and pass new auxiliary array to
			self.sumTriangle(&auxiliary, n - 1);
			i = 0;
			// Display calculated result
			while (i < n)
			{
				print("  ", arr[i], terminator: "");
				i += 1;
			}
			print(terminator: "\n");
		}
	}
}
func main()
{
	let task: Triangle = Triangle();
	// Given array elements
	var arr: [Int] = [1, 4, 3, 1, 6, 5];
	/*
	---------------------------------
	               96  
	            45    51  
	         23    22    29  
	      12    11    11    18  
	   5     7     4     7     11  
	1     4     3     1     6      5   
	---------------------------------
	*/
	// Get the size
	let n: Int = arr.count;
	// Test
	task.sumTriangle(&arr, n);
}
main();

input

   96
   45   51
   23   22   29
   12   11   11   18
   5   7   4   7   11
   1   4   3   1   6   5
/*
  Scala Program for
  Sum triangle of array using recursion 
*/
class Triangle()
{
	// Print the sum triangle by recursion
	def sumTriangle(arr: Array[Int], n: Int): Unit = {
		if (n < 1)
		{
			// Stop recursion
			return;
		}
		else
		{
			// Define auxiliary array
			var auxiliary: Array[Int] = Array.fill[Int](n - 1)(0);
			var sum: Int = 0;
			var i: Int = 0;
			// Execute the loop through by n
			while (i < n - 1)
			{
				// Calculate sum the elements of two consecutive elements
				sum = arr(i) + arr(i + 1);
				// Put sum
				auxiliary(i) = sum;
				i += 1;
			}
			// Reduce size and pass new auxiliary array to
			sumTriangle(auxiliary, n - 1);
			i = 0;
			// Display calculated result
			while (i < n)
			{
				print("  " + arr(i));
				i += 1;
			}
			print("\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Triangle = new Triangle();
		// Given array elements
		var arr: Array[Int] = Array(1, 4, 3, 1, 6, 5);
		/*
		---------------------------------
		               96  
		            45    51  
		         23    22    29  
		      12    11    11    18  
		   5     7     4     7     11  
		1     4     3     1     6      5   
		---------------------------------
		*/
		// Get the size
		var n: Int = arr.length;
		// Test
		task.sumTriangle(arr, n);
	}
}

input

  96
  45  51
  23  22  29
  12  11  11  18
  5  7  4  7  11
  1  4  3  1  6  5


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