Print the hollow numerical parallelogram

Here given code implementation process.

//  C program for
//  Print the hollow numerical parallelogram
#include <stdio.h>
 // This is display empty space of given length
void includeSpace(int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf(" ");
	}
}
void printParallelogram(int n)
{
	printf("\n Given n : %d \n", n);
	// Top half elements
	for (int i = 0; i < n; ++i)
	{
		for (int j = n; j > i; --j)
		{
			printf("%d", n - i);
		}
		// Include space
		includeSpace(i * 2);
		for (int k = n; k > i; --k)
		{
			printf("%d", n - i);
		}
		printf("\n");
	}
	// Bottom half elements
	for (int i = 2; i <= n; ++i)
	{
		for (int j = 1; j <= i; ++j)
		{
			printf("%d", i);
		}
		// Include space
		includeSpace((n - i) * 2);
		for (int j = 1; j <= i; ++j)
		{
			printf("%d", i);
		}
		printf("\n");
	}
}
int main(int argc, char const *argv[])
{
	// Test
	printParallelogram(6);
	printParallelogram(9);
	return 0;
}

Output

 Given n : 6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n : 9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
/*
    Java Program
    Print the hollow numerical parallelogram
*/
public class Pattern
{
	// This is display empty space of given length
	public void includeSpace(int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" ");
		}
	}
	public void printParallelogram(int n)
	{
		System.out.print("\n Given n : " + n + " \n");
		// Top half elements
		for (int i = 0; i < n; ++i)
		{
			for (int j = n; j > i; --j)
			{
				System.out.print(n - i);
			}
			// Include space
			includeSpace(i * 2);
			for (int k = n; k > i; --k)
			{
				System.out.print(n - i);
			}
			System.out.print("\n");
		}
		// Bottom half elements
		for (int i = 2; i <= n; ++i)
		{
			for (int j = 1; j <= i; ++j)
			{
				System.out.print(i);
			}
			// Include space
			includeSpace((n - i) * 2);
			for (int j = 1; j <= i; ++j)
			{
				System.out.print(i);
			}
			System.out.print("\n");
		}
	}
	public static void main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printParallelogram(6);
		task.printParallelogram(9);
	}
}

Output

 Given n : 6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n : 9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Print the hollow numerical parallelogram
*/
class Pattern
{
	public:
		// This is display empty space of given length
		void includeSpace(int n)
		{
			for (int i = 0; i < n; ++i)
			{
				cout << " ";
			}
		}
	void printParallelogram(int n)
	{
		cout << "\n Given n : " << n << " \n";
		// Top half elements
		for (int i = 0; i < n; ++i)
		{
			for (int j = n; j > i; --j)
			{
				cout << n - i;
			}
			// Include space
			this->includeSpace(i *2);
			for (int k = n; k > i; --k)
			{
				cout << n - i;
			}
			cout << "\n";
		}
		// Bottom half elements
		for (int i = 2; i <= n; ++i)
		{
			for (int j = 1; j <= i; ++j)
			{
				cout << i;
			}
			// Include space
			this->includeSpace((n - i) *2);
			for (int j = 1; j <= i; ++j)
			{
				cout << i;
			}
			cout << "\n";
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printParallelogram(6);
	task->printParallelogram(9);
	return 0;
}

Output

 Given n : 6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n : 9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
// Include namespace system
using System;
/*
    Csharp Program
    Print the hollow numerical parallelogram
*/
public class Pattern
{
	// This is display empty space of given length
	public void includeSpace(int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" ");
		}
	}
	public void printParallelogram(int n)
	{
		Console.Write("\n Given n : " + n + " \n");
		// Top half elements
		for (int i = 0; i < n; ++i)
		{
			for (int j = n; j > i; --j)
			{
				Console.Write(n - i);
			}
			// Include space
			this.includeSpace(i * 2);
			for (int k = n; k > i; --k)
			{
				Console.Write(n - i);
			}
			Console.Write("\n");
		}
		// Bottom half elements
		for (int i = 2; i <= n; ++i)
		{
			for (int j = 1; j <= i; ++j)
			{
				Console.Write(i);
			}
			// Include space
			this.includeSpace((n - i) * 2);
			for (int j = 1; j <= i; ++j)
			{
				Console.Write(i);
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printParallelogram(6);
		task.printParallelogram(9);
	}
}

Output

 Given n : 6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n : 9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
package main
import "fmt"
/*
    Go Program
    Print the hollow numerical parallelogram
*/
type Pattern struct {}
func getPattern() * Pattern {
	var me *Pattern = &Pattern {}
	return me
}
// This is display empty space of given length
func(this Pattern) includeSpace(n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ")
	}
}
func(this Pattern) printParallelogram(n int) {
	fmt.Print("\n Given n : ", n, " \n")
	// Top half elements
	for i := 0 ; i < n ; i++ {
		for j := n ; j > i ; j-- {
			fmt.Print(n - i)
		}
		// Include space
		this.includeSpace(i * 2)
		for k := n ; k > i ; k-- {
			fmt.Print(n - i)
		}
		fmt.Print("\n")
	}
	// Bottom half elements
	for i := 2 ; i <= n ; i++ {
		for j := 1 ; j <= i ; j++ {
			fmt.Print(i)
		}
		// Include space
		this.includeSpace((n - i) * 2)
		for j := 1 ; j <= i ; j++ {
			fmt.Print(i)
		}
		fmt.Print("\n")
	}
}
func main() {
	var task * Pattern = getPattern()
	// Test
	task.printParallelogram(6)
	task.printParallelogram(9)
}

Output

 Given n : 6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n : 9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
<?php
/*
    Php Program
    Print the hollow numerical parallelogram
*/
class Pattern
{
	// This is display empty space of given length
	public	function includeSpace($n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ");
		}
	}
	public	function printParallelogram($n)
	{
		echo("\n Given n : ".$n.
			" \n");
		// Top half elements
		for ($i = 0; $i < $n; ++$i)
		{
			for ($j = $n; $j > $i; --$j)
			{
				echo($n - $i);
			}
			// Include space
			$this->includeSpace($i * 2);
			for ($k = $n; $k > $i; --$k)
			{
				echo($n - $i);
			}
			echo("\n");
		}
		// Bottom half elements
		for ($i = 2; $i <= $n; ++$i)
		{
			for ($j = 1; $j <= $i; ++$j)
			{
				echo($i);
			}
			// Include space
			$this->includeSpace(($n - $i) * 2);
			for ($j = 1; $j <= $i; ++$j)
			{
				echo($i);
			}
			echo("\n");
		}
	}
}

function main()
{
	$task = new Pattern();
	// Test
	$task->printParallelogram(6);
	$task->printParallelogram(9);
}
main();

Output

 Given n : 6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n : 9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
/*
    Node JS Program
    Print the hollow numerical parallelogram
*/
class Pattern
{
	// This is display empty space of given length
	includeSpace(n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" ");
		}
	}
	printParallelogram(n)
	{
		process.stdout.write("\n Given n : " + n + " \n");
		// Top half elements
		for (var i = 0; i < n; ++i)
		{
			for (var j = n; j > i; --j)
			{
				process.stdout.write("" + (n - i));
			}
			// Include space
			this.includeSpace(i * 2);
			for (var k = n; k > i; --k)
			{
				process.stdout.write("" + (n - i));
			}
			process.stdout.write("\n");
		}
		// Bottom half elements
		for (var i = 2; i <= n; ++i)
		{
			for (var j = 1; j <= i; ++j)
			{
				process.stdout.write("" + i);
			}
			// Include space
			this.includeSpace((n - i) * 2);
			for (var j = 1; j <= i; ++j)
			{
				process.stdout.write("" + i);
			}
			process.stdout.write("\n");
		}
	}
}

function main()
{
	var task = new Pattern();
	// Test
	task.printParallelogram(6);
	task.printParallelogram(9);
}
main();

Output

 Given n : 6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n : 9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
#    Python 3 Program
#    Print the hollow numerical parallelogram
class Pattern :
	#  This is display empty space of given length
	def includeSpace(self, n) :
		i = 0
		while (i < n) :
			print(" ", end = "")
			i += 1
		
	
	def printParallelogram(self, n) :
		print("\n Given n : ", n ," ")
		i = 0
		#  Top half elements
		while (i < n) :
			j = n
			while (j > i) :
				print(n - i, end = "")
				j -= 1
			
			#  Include space
			self.includeSpace(i * 2)
			k = n
			while (k > i) :
				print(n - i, end = "")
				k -= 1
			
			print(end = "\n")
			i += 1
		
		i = 2
		#  Bottom half elements
		while (i <= n) :
			j = 1
			while (j <= i) :
				print(i, end = "")
				j += 1
			
			#  Include space
			self.includeSpace((n - i) * 2)
			j = 1
			while (j <= i) :
				print(i, end = "")
				j += 1
			
			print(end = "\n")
			i += 1
		
	

def main() :
	task = Pattern()
	#  Test
	task.printParallelogram(6)
	task.printParallelogram(9)

if __name__ == "__main__": main()

Output

 Given n :  6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n :  9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
#    Ruby Program
#    Print the hollow numerical parallelogram
class Pattern 
	#  This is display empty space of given length
	def includeSpace(n) 
		i = 0
		while (i < n) 
			print(" ")
			i += 1
		end

	end

	def printParallelogram(n) 
		print("\n Given n : ", n ," \n")
		i = 0
		#  Top half elements
		while (i < n) 
			j = n
			while (j > i) 
				print(n - i)
				j -= 1
			end

			#  Include space
			self.includeSpace(i * 2)
			k = n
			while (k > i) 
				print(n - i)
				k -= 1
			end

			print("\n")
			i += 1
		end

		i = 2
		#  Bottom half elements
		while (i <= n) 
			j = 1
			while (j <= i) 
				print(i)
				j += 1
			end

			#  Include space
			self.includeSpace((n - i) * 2)
			j = 1
			while (j <= i) 
				print(i)
				j += 1
			end

			print("\n")
			i += 1
		end

	end

end

def main() 
	task = Pattern.new()
	#  Test
	task.printParallelogram(6)
	task.printParallelogram(9)
end

main()

Output

 Given n : 6 
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n : 9 
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
/*
    Scala Program
    Print the hollow numerical parallelogram
*/
class Pattern()
{
	// This is display empty space of given length
	def includeSpace(n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" ");
			i += 1;
		}
	}
	def printParallelogram(n: Int): Unit = {
		print("\n Given n : " + n + " \n");
		var i: Int = 0;
		// Top half elements
		while (i < n)
		{
			var j: Int = n;
			while (j > i)
			{
				print(n - i);
				j -= 1;
			}
			// Include space
			includeSpace(i * 2);
			var k: Int = n;
			while (k > i)
			{
				print(n - i);
				k -= 1;
			}
			print("\n");
			i += 1;
		}
		i = 2;
		// Bottom half elements
		while (i <= n)
		{
			var j: Int = 1;
			while (j <= i)
			{
				print(i);
				j += 1;
			}
			// Include space
			includeSpace((n - i) * 2);
			j = 1;
			while (j <= i)
			{
				print(i);
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pattern = new Pattern();
		// Test
		task.printParallelogram(6);
		task.printParallelogram(9);
	}
}

Output

 Given n : 6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n : 9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
/*
    Swift 4 Program
    Print the hollow numerical parallelogram
*/
class Pattern
{
	// This is display empty space of given length
	func includeSpace(_ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", terminator: "");
			i += 1;
		}
	}
	func printParallelogram(_ n: Int)
	{
		print("\n Given n : ", n ," ");
		var i: Int = 0;
		// Top half elements
		while (i < n)
		{
			var j: Int = n;
			while (j > i)
			{
				print(n - i, terminator: "");
				j -= 1;
			}
			// Include space
			self.includeSpace(i * 2);
			var k: Int = n;
			while (k > i)
			{
				print(n - i, terminator: "");
				k -= 1;
			}
			print(terminator: "\n");
			i += 1;
		}
		i = 2;
		// Bottom half elements
		while (i <= n)
		{
			var j: Int = 1;
			while (j <= i)
			{
				print(i, terminator: "");
				j += 1;
			}
			// Include space
			self.includeSpace((n - i) * 2);
			j = 1;
			while (j <= i)
			{
				print(i, terminator: "");
				j += 1;
			}
			print(terminator: "\n");
			i += 1;
		}
	}
}
func main()
{
	let task: Pattern = Pattern();
	// Test
	task.printParallelogram(6);
	task.printParallelogram(9);
}
main();

Output

 Given n :  6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n :  9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999
/*
    Kotlin Program
    Print the hollow numerical parallelogram
*/
class Pattern
{
	// This is display empty space of given length
	fun includeSpace(n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ");
			i += 1;
		}
	}
	fun printParallelogram(n: Int): Unit
	{
		print("\n Given n : " + n + " \n");
		var i: Int = 0;
		// Top half elements
		while (i < n)
		{
			var j: Int = n;
			while (j > i)
			{
				print(n - i);
				j -= 1;
			}
			// Include space
			this.includeSpace(i * 2);
			var k: Int = n;
			while (k > i)
			{
				print(n - i);
				k -= 1;
			}
			print("\n");
			i += 1;
		}
		i = 2;
		// Bottom half elements
		while (i <= n)
		{
			var j: Int = 1;
			while (j <= i)
			{
				print(i);
				j += 1;
			}
			// Include space
			this.includeSpace((n - i) * 2);
			j = 1;
			while (j <= i)
			{
				print(i);
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pattern = Pattern();
	// Test
	task.printParallelogram(6);
	task.printParallelogram(9);
}

Output

 Given n : 6
666666666666
55555  55555
4444    4444
333      333
22        22
1          1
22        22
333      333
4444    4444
55555  55555
666666666666

 Given n : 9
999999999999999999
88888888  88888888
7777777    7777777
666666      666666
55555        55555
4444          4444
333            333
22              22
1                1
22              22
333            333
4444          4444
55555        55555
666666      666666
7777777    7777777
88888888  88888888
999999999999999999


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