Display leibniz harmonic Triangle

Here given code implementation process.

//C Program 
//Display leibniz harmonic Triangle
#include <stdio.h>

//Display result of leibniz harmonic triangle
void show(int *result, int size)
{
	int i = 0, j = 0;
	for (i = 1; i <= size; ++i)
	{
		for (j = i; j < size; ++j)
		{
			printf("\t");
		}
		for (j = 1; j <= i; ++j)
		{
			if (i == 1 && j == 1)
			{
				printf("%d", 1);
			}
			else
			{
				//in 2d array result[i-1][j-1]*i
				printf("1/%d\t\t", *((result + (i - 1) *(size + 1) + (j - 1))) *i);
			}
		}
		printf("\n");
	}
}
void harmonic_triangle(int size)
{
	printf("\n ROW  SIZE : %d\n", size);
	//Create a 2d array, which is store the result
	int result[size + 1][size + 1];
	int i = 0, j = 0;
	for (i = 0; i <= size; ++i)
	{
		for (j = 0; j <= i; ++j)
		{
			if (j == i || j == 0)
			{
				//Simple base case when need to set 1
				result[i][j] = 1;
			}
			else
			{
				result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
			}
		}
	}
	show((int *) result, size);
}
int main()
{
	//Test Cases
	harmonic_triangle(5);
	harmonic_triangle(7);
	return 0;
}

Output

 ROW  SIZE : 5
				1
			1/2		1/2
		1/3		1/6		1/3
	1/4		1/12		1/12		1/4
1/5		1/20		1/30		1/20		1/5

 ROW  SIZE : 7
						1
					1/2		1/2
				1/3		1/6		1/3
			1/4		1/12		1/12		1/4
		1/5		1/20		1/30		1/20		1/5
	1/6		1/30		1/60		1/60		1/30		1/6
1/7		1/42		1/105		1/140		1/105		1/42		1/7
/*
  Java Program
  Display leibniz harmonic Triangle
*/
class MyPattern
{
	//Display result of leibniz harmonic triangle
	public void show(int[][] result, int size)
	{
		int i = 0, j = 0;
		for (i = 1; i <= size; ++i)
		{
			for (j = i; j < size; ++j)
			{
				System.out.print("\t");
			}
			for (j = 1; j <= i; ++j)
			{
				if (i == 1 && j == 1)
				{
					System.out.print("1");
				}
				else
				{
					System.out.print("1/" + result[i - 1][j - 1] * i + "\t\t");
				}
			}
			System.out.print("\n");
		}
	}
	public void harmonic_triangle(int size)
	{
		System.out.print("\n ROW SIZE : " + size + "\n");
		//Create a 2d array, which is store the result
		int result[][] = new int[size + 1][size + 1];
		int i = 0, j = 0;
		for (i = 0; i <= size; ++i)
		{
			for (j = 0; j <= i; ++j)
			{
				if (j == i || j == 0)
				{
					//Simple base case when need to set 1
					result[i][j] = 1;
				}
				else
				{
					result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
				}
			}
		}
		show(result, size);
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.harmonic_triangle(5);
		obj.harmonic_triangle(7);
	}
}

Output

 ROW SIZE : 5
				1
			1/2		1/2
		1/3		1/6		1/3
	1/4		1/12		1/12		1/4
1/5		1/20		1/30		1/20		1/5

 ROW SIZE : 7
						1
					1/2		1/2
				1/3		1/6		1/3
			1/4		1/12		1/12		1/4
		1/5		1/20		1/30		1/20		1/5
	1/6		1/30		1/60		1/60		1/30		1/6
1/7		1/42		1/105		1/140		1/105		1/42		1/7
/*
  C++ Program
  Display leibniz harmonic Triangle
*/
#include<iostream>

using namespace std;
class MyPattern
{
	public:
		//Display result of leibniz harmonic triangle
		void show(int *result, int size)
		{
			int i = 0, j = 0;
			for (i = 1; i <= size; ++i)
			{
				for (j = i; j < size; ++j)
				{
					cout << "\t";
				}
				for (j = 1; j <= i; ++j)
				{
					if (i == 1 && j == 1)
					{
						cout << "1";
					}
					else
					{
						cout << "1/" << *((result + (i - 1) *(size + 1) + (j - 1))) *i << "\t\t";
					}
				}
				cout << "\n";
			}
		}
	void harmonic_triangle(int size)
	{
		cout << "\n ROW SIZE : " << size << "\n";
		//Create a 2d array, which is store the result
		int result[size + 1][size + 1];
		int i = 0, j = 0;
		for (i = 0; i <= size; ++i)
		{
			for (j = 0; j <= i; ++j)
			{
				if (j == i || j == 0)
				{
					//Simple base case when need to set 1
					result[i][j] = 1;
				}
				else
				{
					result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
				}
			}
		}
		this->show((int*)result, size);
	}
};
int main()
{
	MyPattern obj =  MyPattern();
	//Test Cases
	obj.harmonic_triangle(5);
	obj.harmonic_triangle(7);
	return 0;
}

Output

 ROW SIZE : 5
				1
			1/2		1/2
		1/3		1/6		1/3
	1/4		1/12		1/12		1/4
1/5		1/20		1/30		1/20		1/5

 ROW SIZE : 7
						1
					1/2		1/2
				1/3		1/6		1/3
			1/4		1/12		1/12		1/4
		1/5		1/20		1/30		1/20		1/5
	1/6		1/30		1/60		1/60		1/30		1/6
1/7		1/42		1/105		1/140		1/105		1/42		1/7
/*
  C# Program
  Display leibniz harmonic Triangle
*/
using System;
class MyPattern
{
	//Display result of leibniz harmonic triangle
	public void show(int[,] result, int size)
	{
		int i = 0, j = 0;
		for (i = 1; i <= size; i++)
		{
			for (j = i; j < size; j++)
			{
				Console.Write("\t");
			}
			for (j = 1; j <= i; j++)
			{
				if (i == 1 && j == 1)
				{
					Console.Write("1");
				}
				else
				{
					Console.Write("1/" + result[i - 1,j - 1] * i + "\t\t");
				}
			}
			Console.Write("\n");
		}
	}
	public void harmonic_triangle(int size)
	{
		Console.Write("\n ROW SIZE : " + size + "\n");
		int[,] result = new int[size + 1,size + 1];
		int i = 0, j = 0;
		for (i = 0; i <= size; i++)
		{
			for (j = 0; j <= i; j++)
			{
				if (j == i || j == 0)
				{
					//Simple base case when need to set 1
					result[i,j] = 1;
				}
				else
				{
					result[i,j] = result[i - 1,j - 1] + result[i - 1,j];
				}
			}
		}
		show(result, size);
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.harmonic_triangle(5);
		obj.harmonic_triangle(7);
	}
}

Output

 ROW SIZE : 5
				1
			1/2		1/2
		1/3		1/6		1/3
	1/4		1/12		1/12		1/4
1/5		1/20		1/30		1/20		1/5

 ROW SIZE : 7
						1
					1/2		1/2
				1/3		1/6		1/3
			1/4		1/12		1/12		1/4
		1/5		1/20		1/30		1/20		1/5
	1/6		1/30		1/60		1/60		1/30		1/6
1/7		1/42		1/105		1/140		1/105		1/42		1/7
<?php
/*
  Php Program
  Display leibniz harmonic Triangle
*/
class MyPattern
{
	//Display result of leibniz harmonic triangle
	public	function show( $result, $size)
	{
		$i = 0;
		$j = 0;
		for ($i = 1; $i <= $size; ++$i)
		{
			for ($j = $i; $j < $size; ++$j)
			{
				echo("\t");
			}
			for ($j = 1; $j <= $i; ++$j)
			{
				if ($i == 1 && $j == 1)
				{
					echo("1");
				}
				else
				{
					echo("1/". $result[$i - 1][$j - 1] * $i ."\t\t");
				}
			}
			echo("\n");
		}
	}
	public	function harmonic_triangle($size)
	{
		echo("\n ROW SIZE : ". $size ."\n");
		//Create a 2d array, which is store the result
		$result = array_fill(0, $size + 1,  array_fill(0, $size + 1, 0));
		$i = 0;
		$j = 0;
		for ($i = 0; $i <= $size; ++$i)
		{
			for ($j = 0; $j <= $i; ++$j)
			{
				if ($j == $i || $j == 0)
				{
					//Simple base case when need to set 1
					$result[$i][$j] = 1;
				}
				else
				{
					$result[$i][$j] = $result[$i - 1][$j - 1] + $result[$i - 1][$j];
				}
			}
		}
		$this->show($result, $size);
	}
}

function main()
{
	$obj = new MyPattern();
	//Test Cases
	$obj->harmonic_triangle(5);
	$obj->harmonic_triangle(7);
}
main();

Output

 ROW SIZE : 5
				1
			1/2		1/2
		1/3		1/6		1/3
	1/4		1/12		1/12		1/4
1/5		1/20		1/30		1/20		1/5

 ROW SIZE : 7
						1
					1/2		1/2
				1/3		1/6		1/3
			1/4		1/12		1/12		1/4
		1/5		1/20		1/30		1/20		1/5
	1/6		1/30		1/60		1/60		1/30		1/6
1/7		1/42		1/105		1/140		1/105		1/42		1/7
/*
  Node Js Program
  Display leibniz harmonic Triangle
*/
class MyPattern
{
	//Display result of leibniz harmonic triangle
	show(result, size)
	{
		var i = 0;
		var j = 0;
		for (i = 1; i <= size; ++i)
		{
			for (j = i; j < size; ++j)
			{
				process.stdout.write("\t");
			}
			for (j = 1; j <= i; ++j)
			{
				if (i == 1 && j == 1)
				{
					process.stdout.write("1");
				}
				else
				{
					process.stdout.write("1/" + result[i - 1][j - 1] * i + "\t\t");
				}
			}
			process.stdout.write("\n");
		}
	}
	harmonic_triangle(size)
	{
		process.stdout.write("\n ROW SIZE : " + size + "\n");
		//Create a 2d array, which is store the result
		var result = Array(size + 1).fill(0).map(() => new Array(size + 1).fill(0));
		var i = 0;
		var j = 0;
		for (i = 0; i <= size; ++i)
		{
			for (j = 0; j <= i; ++j)
			{
				if (j == i || j == 0)
				{
					//Simple base case when need to set 1
					result[i][j] = 1;
				}
				else
				{
					result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
				}
			}
		}
		this.show(result, size);
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Test Cases
	obj.harmonic_triangle(5);
	obj.harmonic_triangle(7);
}
main();

Output

 ROW SIZE : 5
				1
			1/2		1/2
		1/3		1/6		1/3
	1/4		1/12		1/12		1/4
1/5		1/20		1/30		1/20		1/5

 ROW SIZE : 7
						1
					1/2		1/2
				1/3		1/6		1/3
			1/4		1/12		1/12		1/4
		1/5		1/20		1/30		1/20		1/5
	1/6		1/30		1/60		1/60		1/30		1/6
1/7		1/42		1/105		1/140		1/105		1/42		1/7
#   Python 3 Program
#   Display leibniz harmonic Triangle

class MyPattern :
	# Display result of leibniz harmonic triangle
	def show(self, result, size) :
		i = 1
		j = 1
		while (i <= size) :
			j = i
			while (j < size) :
				print("\t", end = "")
				j += 1
			
			j = 1
			while (j <= i) :
				if (i == 1 and j == 1) :
					print("1", end = "")
				else :
					print("1/", result[i - 1][j - 1] * i ,"\t\t", end = "")
				
				j += 1
			
			print("\n", end = "")
			i += 1
		
	
	def harmonic_triangle(self, size) :
		print("\n ROW SIZE : ", size ,"\n", end = "")
		result = [[0 for _ in range(size+1)] for _ in range(size+1)]
		i = 0
		j = 0
		while (i <= size) :
			j = 0
			while (j <= i) :
				if (j == i or j == 0) :
					# Simple base case when need to set 1
					result[i][j] = 1
				else :
					result[i][j] = result[i - 1][j - 1] + result[i - 1][j]
				
				j += 1
			
			i += 1
		
		self.show(result, size)
	

def main() :
	obj = MyPattern()
	# Test Cases
	obj.harmonic_triangle(5)
	obj.harmonic_triangle(7)


if __name__ == "__main__": main()

Output

 ROW SIZE :  5
				1
			1/ 2 		1/ 2
		1/ 3 		1/ 6 		1/ 3
	1/ 4 		1/ 12 		1/ 12 		1/ 4
1/ 5 		1/ 20 		1/ 30 		1/ 20 		1/ 5

 ROW SIZE :  7
						1
					1/ 2 		1/ 2
				1/ 3 		1/ 6 		1/ 3
			1/ 4 		1/ 12 		1/ 12 		1/ 4
		1/ 5 		1/ 20 		1/ 30 		1/ 20 		1/ 5
	1/ 6 		1/ 30 		1/ 60 		1/ 60 		1/ 30 		1/ 6
1/ 7 		1/ 42 		1/ 105 		1/ 140 		1/ 105 		1/ 42 		1/ 7
#   Ruby Program
#   Display leibniz harmonic Triangle

class MyPattern

	# Display result of leibniz harmonic triangle
	def show(result, size)
	
		i = 1
		j = 1
		while (i <= size)
		
			j = i
			while (j < size)
			
				print("\t")
				j += 1
			end
			j = 1
			while (j <= i)
			
				if (i == 1 && j == 1)
				
					print("1")
				else
				
					print("1/", result[i - 1][j - 1] * i ,"\t\t")
				end
				j += 1
			end
			print("\n")
			i += 1
		end
	end
	def harmonic_triangle(size)
	
		print("\n ROW SIZE : ", size ,"\n")
		result = Array.new(size + 1) {Array.new(size + 1) }
		i = 0
		j = 0
		while (i <= size)
		
			j = 0
			while (j <= i)
			
				if (j == i || j == 0)
				
					# Simple base case when need to set 1
					result[i][j] = 1
				else
				
					result[i][j] = result[i - 1][j - 1] + result[i - 1][j]
				end
				j += 1
			end
			i += 1
		end
		self.show(result, size)
	end
end
def main()

	obj = MyPattern.new()
	# Test Cases
	obj.harmonic_triangle(5)
	obj.harmonic_triangle(7)
end
main()

Output

 ROW SIZE : 5
				1
			1/2		1/2		
		1/3		1/6		1/3		
	1/4		1/12		1/12		1/4		
1/5		1/20		1/30		1/20		1/5		

 ROW SIZE : 7
						1
					1/2		1/2		
				1/3		1/6		1/3		
			1/4		1/12		1/12		1/4		
		1/5		1/20		1/30		1/20		1/5		
	1/6		1/30		1/60		1/60		1/30		1/6		
1/7		1/42		1/105		1/140		1/105		1/42		1/7		
/*
  Scala Program
  Display leibniz harmonic Triangle
*/
class MyPattern
{
	//Display result of leibniz harmonic triangle
	def show(result: Array[Array[Int]], size: Int): Unit = {
		var i: Int = 1;
		var j: Int = 1;
		while (i <= size)
		{
			j = i;
			while (j < size)
			{
				print("\t");
				j += 1;
			}
			j = 1;
			while (j <= i)
			{
				if (i == 1 && j == 1)
				{
					print("1");
				}
				else
				{
					print("1/" + result(i - 1)(j - 1) * i + "\t\t");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
	def harmonic_triangle(size: Int): Unit = {
		print("\n ROW SIZE : " + size + "\n");
		var result: Array[Array[Int]] = Array.fill[Int](size + 1,size+1)(0);
		var i: Int = 0;
		var j: Int = 0;
		while (i <= size)
		{
			j = 0;
			while (j <= i)
			{
				if (j == i || j == 0)
				{
					//Simple base case when need to set 1
					result(i)(j) = 1;
				}
				else
				{
					result(i)(j) = result(i - 1)(j - 1) + result(i - 1)(j);
				}
				j += 1;
			}
			i += 1;
		}
		show(result, size);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test Cases
		obj.harmonic_triangle(5);
		obj.harmonic_triangle(7);
	}
}

Output

 ROW SIZE : 5
				1
			1/2		1/2
		1/3		1/6		1/3
	1/4		1/12		1/12		1/4
1/5		1/20		1/30		1/20		1/5

 ROW SIZE : 7
						1
					1/2		1/2
				1/3		1/6		1/3
			1/4		1/12		1/12		1/4
		1/5		1/20		1/30		1/20		1/5
	1/6		1/30		1/60		1/60		1/30		1/6
1/7		1/42		1/105		1/140		1/105		1/42		1/7
/*
  Swift Program
  Display leibniz harmonic Triangle
*/
class MyPattern
{
	//Display result of leibniz harmonic triangle
	func show(_ result: [
		[Int]
	], _ size: Int)
	{
		var i: Int = 1;
		var j: Int = 1;
		while (i <= size)
		{
			j = i;
			while (j < size)
			{
				print("\t", terminator: "");
				j += 1;
			}
			j = 1;
			while (j <= i)
			{
				if (i == 1 && j == 1)
				{
					print("1", terminator: "");
				}
				else
				{
					print("1/", result[i - 1][j - 1] * i , terminator: "\t\t");
				}
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
	func harmonic_triangle(_ size: Int)
	{
		print("\n ROW SIZE : ", size ,"\n", terminator: "");
		var result: [
			[Int]
		] = Array(repeating: Array(repeating: 0, count: size + 1), count: size + 1);
		var i: Int = 0;
		var j: Int = 0;
		while (i <= size)
		{
			j = 0;
			while (j <= i)
			{
				if (j == i || j == 0)
				{
					//Simple base case when need to set 1
					result[i][j] = 1;
				}
				else
				{
					result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
				}
				j += 1;
			}
			i += 1;
		}
		self.show(result, size);
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test Cases
	obj.harmonic_triangle(5);
	obj.harmonic_triangle(7);
}
main();

Output

 ROW SIZE :  5
				1
			1/ 2		1/ 2
		1/ 3		1/ 6		1/ 3
	1/ 4		1/ 12		1/ 12		1/ 4
1/ 5		1/ 20		1/ 30		1/ 20		1/ 5

 ROW SIZE :  7
						1
					1/ 2		1/ 2
				1/ 3		1/ 6		1/ 3
			1/ 4		1/ 12		1/ 12		1/ 4
		1/ 5		1/ 20		1/ 30		1/ 20		1/ 5
	1/ 6		1/ 30		1/ 60		1/ 60		1/ 30		1/ 6
1/ 7		1/ 42		1/ 105		1/ 140		1/ 105		1/ 42		1/ 7


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