Print the hollow numerical parallelogram
In this article, we will discuss a program to print a hollow numerical parallelogram. A numerical parallelogram is a pattern of numbers arranged in a parallelogram shape. The program will take an input "n" and print a parallelogram with numbers ranging from "n" to 1 and back to "n" again, forming a hollow pattern.
Problem Statement
The task is to write a program that prints a hollow numerical parallelogram pattern using the given input "n". The pattern should consist of numbers in a descending order from "n" to 1 and then ascending back to "n". The pattern should be hollow, with spaces in the inner area.
For example, if the input is 6, the output will be:
Given n : 6 666666666666 55555 55555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 55555 55555 666666666666
Algorithm
The program follows the following algorithm to print the hollow numerical parallelogram pattern:
- Define a function includeSpace() to print a specified number of empty spaces.
- Define a function printParallelogram() that takes the input "n".
- Print the given input "n".
- Print the top half of the parallelogram:
- Loop from i = 0 to i < n.
- Within the outer loop, loop from j = n to j > i and print (n - i) in each iteration.
- Call the includeSpace() function with the argument (i * 2) to print the required number of empty spaces.
- Within the outer loop, loop from k = n to k > i and print (n - i) in each iteration.
- Print a new line.
- Print the bottom half of the parallelogram:
- Loop from i = 2 to i <= n.
- Within the outer loop, loop from j = 1 to j <= i and print i in each iteration.
- Call the includeSpace() function with the argument ((n - i) * 2) to print the required number of empty spaces.
- Within the outer loop, loop from j = 1 to j <= i and print i in each iteration.
- Print a new line.
- End the function printParallelogram().
- In the main() function, test the printParallelogram() function with different inputs.
- Return 0 to indicate successful program execution.
Pseudocode
function includeSpace(n)
for i = 0 to i < n
print " "
end for
end function
function printParallelogram(n)
print "Given n: " + n
for i = 0 to i < n
for j = n to j > i
print (n - i)
end for
includeSpace(i * 2)
for k = n to k > i
print (n - i)
end for
print new line
end for
for i = 2 to i <= n
for j = 1 to j <= i
print i
end for
includeSpace((n - i) * 2)
for j = 1 to j <= i
print i
end for
print new line
end for
end function
function main()
printParallelogram(6)
printParallelogram(9)
return 0
end function
Explanation
The program first defines a function includeSpace() to print a specified number of empty spaces. This function is used to create the hollow spaces inside the parallelogram.
The main function of the program is printParallelogram(). This function takes the input "n" and prints the numerical parallelogram pattern.
The function starts by printing "Given n: " followed by the value of "n".
Next, it prints the top half of the parallelogram using nested loops. The outer loop runs from 0 to n - 1, and the inner loops print the numbers in descending order from n to (n - i) and ascending order from (n - i) to n. Between the inner loops, the includeSpace() function is called to print the required number of empty spaces.
After printing the top half, the function moves to the bottom half of the parallelogram using another set of nested loops. The outer loop runs from 2 to n, and the inner loops print the numbers in ascending order from 1 to i and descending order from i to 1. Again, the includeSpace() function is called to print the appropriate number of empty spaces.
The main() function tests the printParallelogram() function by calling it with different inputs, in this case, 6 and 9.
Code Solution
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
The time complexity of the code is O(n^2) because there are two nested loops that iterate up to the value of "n" in both the top and bottom halves of the parallelogram pattern.
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