Tiling problem using dynamic programming

Here given code implementation process.

// C Program
// Tiling problem dynamic programming
#include <stdio.h>

void findResult(int n)
{
    if(n <= 0)
    {
        return ;
    }
    int dp[n+1];
    // Set initial values
    dp[0] = 0; 
    dp[1] = 1;

    // Calualte result
    for(int i=2; i<= n;++i)
    {
        dp[i]  = dp[i-1] + dp[i-2];
    }
    // Display given value
    printf("\n Given  : %d", n);
    // Display calculated result
    printf("\n Result : %d",dp[n]);
}
int main()
{
    
    // Test case
    findResult(7);
    findResult(3);
    return 0;
}

Output

 Given  : 7
 Result : 13
 Given  : 3
 Result : 2
// Java program for
// Tiling problem dynamic programming
public class TilingProblem
{
	public void findResult(int n)
	{
		if (n <= 0)
		{
			return;
		}
		int[] dp = new int[n + 1];
		// Set initial values
		dp[0] = 0;
		dp[1] = 1;
		// Calualte result
		for (int i = 2; i <= n; ++i)
		{
			dp[i] = dp[i - 1] + dp[i - 2];
		}
		// Display given value
		System.out.print("\n Given : " + n);
		// Display calculated result
		System.out.print("\n Result : " + dp[n]);
	}
	public static void main(String[] args)
	{
		TilingProblem task = new TilingProblem();
		// Test
		task.findResult(7);
		task.findResult(3);
	}
}

Output

 Given : 7
 Result : 13
 Given : 3
 Result : 2
// Include header file
#include <iostream>
using namespace std;

// C++ program for
// Tiling problem dynamic programming

class TilingProblem
{
	public: void findResult(int n)
	{
		if (n <= 0)
		{
			return;
		}
		int dp[n + 1];
		// Set initial values
		dp[0] = 0;
		dp[1] = 1;
		// Calualte result
		for (int i = 2; i <= n; ++i)
		{
			dp[i] = dp[i - 1] + dp[i - 2];
		}
		// Display given value
		cout << "\n Given : " << n;
		// Display calculated result
		cout << "\n Result : " << dp[n];
	}
};
int main()
{
	TilingProblem *task = new TilingProblem();
	// Test
	task->findResult(7);
	task->findResult(3);
	return 0;
}

Output

 Given : 7
 Result : 13
 Given : 3
 Result : 2
// Include namespace system
using System;
// Csharp program for
// Tiling problem dynamic programming
public class TilingProblem
{
	public void findResult(int n)
	{
		if (n <= 0)
		{
			return;
		}
		int[] dp = new int[n + 1];
		// Set initial values
		dp[0] = 0;
		dp[1] = 1;
		// Calualte result
		for (int i = 2; i <= n; ++i)
		{
			dp[i] = dp[i - 1] + dp[i - 2];
		}
		// Display given value
		Console.Write("\n Given : " + n);
		// Display calculated result
		Console.Write("\n Result : " + dp[n]);
	}
	public static void Main(String[] args)
	{
		TilingProblem task = new TilingProblem();
		// Test
		task.findResult(7);
		task.findResult(3);
	}
}

Output

 Given : 7
 Result : 13
 Given : 3
 Result : 2
package main
import "fmt"
// Go program for
// Tiling problem dynamic programming

func findResult(n int) {
	if n <= 0 {
		return
	}
	var dp = make([] int, n + 1)
	// Set initial values
	dp[0] = 0
	dp[1] = 1
	// Calualte result
	for i := 2 ; i <= n ; i++ {
		dp[i] = dp[i - 1] + dp[i - 2]
	}
	// Display given value
	fmt.Print("\n Given : ", n)
	// Display calculated result
	fmt.Print("\n Result : ", dp[n])
}
func main() {
	// Test
	findResult(7)
	findResult(3)
}

Output

 Given : 7
 Result : 13
 Given : 3
 Result : 2
<?php
// Php program for
// Tiling problem dynamic programming
class TilingProblem
{
	public	function findResult($n)
	{
		if ($n <= 0)
		{
			return;
		}
		$dp = array_fill(0, $n + 1, 0);
		// Set initial values
		$dp[0] = 0;
		$dp[1] = 1;
		// Calualte result
		for ($i = 2; $i <= $n; ++$i)
		{
			$dp[$i] = $dp[$i - 1] + $dp[$i - 2];
		}
		// Display given value
		echo("\n Given : ".$n);
		// Display calculated result
		echo("\n Result : ".$dp[$n]);
	}
}

function main()
{
	$task = new TilingProblem();
	// Test
	$task->findResult(7);
	$task->findResult(3);
}
main();

Output

 Given : 7
 Result : 13
 Given : 3
 Result : 2
// Node JS program for
// Tiling problem dynamic programming
class TilingProblem
{
	findResult(n)
	{
		if (n <= 0)
		{
			return;
		}
		var dp = Array(n + 1).fill(0);
		// Set initial values
		dp[0] = 0;
		dp[1] = 1;
		// Calualte result
		for (var i = 2; i <= n; ++i)
		{
			dp[i] = dp[i - 1] + dp[i - 2];
		}
		// Display given value
		process.stdout.write("\n Given : " + n);
		// Display calculated result
		process.stdout.write("\n Result : " + dp[n]);
	}
}

function main()
{
	var task = new TilingProblem();
	// Test
	task.findResult(7);
	task.findResult(3);
}
main();

Output

 Given : 7
 Result : 13
 Given : 3
 Result : 2
#  Python 3 program for
#  Tiling problem dynamic programming
class TilingProblem :
	def findResult(self, n) :
		if (n <= 0) :
			return
		
		dp = [0] * (n + 1)
		#  Set initial values
		dp[0] = 0
		dp[1] = 1
		i = 2
		#  Calualte result
		while (i <= n) :
			dp[i] = dp[i - 1] + dp[i - 2]
			i += 1
		
		#  Display given value
		print("\n Given : ", n, end = "")
		#  Display calculated result
		print("\n Result : ", dp[n], end = "")
	

def main() :
	task = TilingProblem()
	#  Test
	task.findResult(7)
	task.findResult(3)

if __name__ == "__main__": main()

Output

 Given :  7
 Result :  13
 Given :  3
 Result :  2
#  Ruby program for
#  Tiling problem dynamic programming
class TilingProblem 
	def findResult(n) 
		if (n <= 0) 
			return
		end

		dp = Array.new(n + 1) {0}
		#  Set initial values
		dp[0] = 0
		dp[1] = 1
		i = 2
		#  Calualte result
		while (i <= n) 
			dp[i] = dp[i - 1] + dp[i - 2]
			i += 1
		end

		#  Display given value
		print("\n Given : ", n)
		#  Display calculated result
		print("\n Result : ", dp[n])
	end

end

def main() 
	task = TilingProblem.new()
	#  Test
	task.findResult(7)
	task.findResult(3)
end

main()

Output

 Given : 7
 Result : 13
 Given : 3
 Result : 2
// Scala program for
// Tiling problem dynamic programming
class TilingProblem()
{
	def findResult(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		var dp: Array[Int] = Array.fill[Int](n + 1)(0);
		// Set initial values
		dp(0) = 0;
		dp(1) = 1;
		var i: Int = 2;
		// Calualte result
		while (i <= n)
		{
			dp(i) = dp(i - 1) + dp(i - 2);
			i += 1;
		}
		// Display given value
		print("\n Given : " + n);
		// Display calculated result
		print("\n Result : " + dp(n));
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: TilingProblem = new TilingProblem();
		// Test
		task.findResult(7);
		task.findResult(3);
	}
}

Output

 Given : 7
 Result : 13
 Given : 3
 Result : 2
// Swift 4 program for
// Tiling problem dynamic programming
class TilingProblem
{
	func findResult(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		var dp: [Int] = Array(repeating: 0, count: n + 1);
		// Set initial values
		dp[0] = 0;
		dp[1] = 1;
		var i: Int = 2;
		// Calualte result
		while (i <= n)
		{
			dp[i] = dp[i - 1] + dp[i - 2];
			i += 1;
		}
		// Display given value
		print("\n Given : ", n, terminator: "");
		// Display calculated result
		print("\n Result : ", dp[n], terminator: "");
	}
}
func main()
{
	let task: TilingProblem = TilingProblem();
	// Test
	task.findResult(7);
	task.findResult(3);
}
main();

Output

 Given :  7
 Result :  13
 Given :  3
 Result :  2
// Kotlin program for
// Tiling problem dynamic programming
class TilingProblem
{
	fun findResult(n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		val dp: Array < Int > = Array(n + 1)
		{
			0
		};
		// Set initial values
		dp[0] = 0;
		dp[1] = 1;
		var i: Int = 2;
		// Calualte result
		while (i <= n)
		{
			dp[i] = dp[i - 1] + dp[i - 2];
			i += 1;
		}
		// Display given value
		print("\n Given : " + n);
		// Display calculated result
		print("\n Result : " + dp[n]);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: TilingProblem = TilingProblem();
	// Test
	task.findResult(7);
	task.findResult(3);
}

Output

 Given : 7
 Result : 13
 Given : 3
 Result : 2


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