Program to print the hollow numerical parallelogram
Program to print the hollow numerical parallelogram.
Examples :
Input : 7
Output :
7777777777777
666666 666666
55555 55555
4444 4444
333 333
22 22
1 1
22 22
333 333
4444 4444
55555 55555
666666 666666
7777777777777
C++
#include <bits/stdc++.h>
using namespace std;
void pattern()
{
int i, j, k = 0, spaces = 1, n = 7;
for (i = n; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
cout << i;
}
if (i != n)
{
for (k = 1; k <= spaces; k++)
{
cout << " " ;
}
spaces = spaces + 2;
}
for (j = i; j >= 1; j--)
{
if (j != n)
cout << i;
}
cout << endl;
}
spaces = spaces - 4;
for (i = 2; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
cout << i;
}
if (i != n)
{
for (k = 1; k <= spaces; k++)
{
cout << " " ;
}
spaces = spaces - 2;
}
for (j = i; j >= 1; j--)
{
if (j != n)
cout << i;
}
cout << endl;
}
}
int main()
{
pattern();
return 0;
}
|
C
#include <stdio.h>
void pattern()
{
int i, j, k = 0, spaces = 1, n = 7;
for (i = n; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf ( "%d" , i);
}
if (i != n) {
for (k = 1; k <= spaces; k++) {
printf ( " " );
}
spaces = spaces + 2;
}
for (j = i; j >= 1; j--) {
if (j != n)
printf ( "%d" , i);
}
printf ( "\n" );
}
spaces = spaces - 4;
for (i = 2; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf ( "%d" , i);
}
if (i != n) {
for (k = 1; k <= spaces; k++) {
printf ( " " );
}
spaces = spaces - 2;
}
for (j = i; j >= 1; j--) {
if (j != n)
printf ( "%d" , i);
}
printf ( "\n" );
}
}
int main()
{
pattern();
return 0;
}
|
Java
import java.io.*;
class GFG {
static void pattern()
{
int i, j, k = 0 , spaces = 1 , n = 7 ;
for (i = n; i >= 1 ; i--) {
for (j = 1 ; j <= i; j++) {
System.out.print(i);
}
if (i != n) {
for (k = 1 ; k <= spaces; k++) {
System.out.print( " " );
}
spaces = spaces + 2 ;
}
for (j = i; j >= 1 ; j--) {
if (j != n)
System.out.print(i);
}
System.out.println();
}
spaces = spaces - 4 ;
for (i = 2 ; i <= n; i++) {
for (j = 1 ; j <= i; j++) {
System.out.print( i);
}
if (i != n) {
for (k = 1 ; k <= spaces; k++) {
System.out.printf( " " );
}
spaces = spaces - 2 ;
}
for (j = i; j >= 1 ; j--) {
if (j != n)
System.out.print(i);
}
System.out.println();
}
}
public static void main (String[] args)
{
pattern();
}
}
|
Python3
def pattern():
k = 0
spaces = 1
n = 7
for i in range (n, 0 , - 1 ):
for j in range ( 1 , i + 1 ):
print (i, end = "")
if (i ! = n):
for k in range ( 1 , spaces + 1 ):
print (end = " " )
spaces = spaces + 2
for j in range (i, 0 , - 1 ):
if (j ! = n):
print (i, end = "")
print ()
spaces = spaces - 4
for i in range ( 2 , n + 1 ):
for j in range ( 1 , i + 1 ):
print (i, end = "")
if (i ! = n):
for k in range ( 1 , spaces + 1 ):
print (end = " " )
spaces = spaces - 2
for j in range (i, 0 , - 1 ):
if (j ! = n):
print (i, end = "")
print ()
pattern()
|
C#
using System;
class GFG
{
static void pattern()
{
int i, j, k = 0;
int spaces = 1, n = 7;
for (i = n; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
Console.Write(i);
}
if (i != n)
{
for (k = 1; k <= spaces; k++)
{
Console.Write( " " );
}
spaces = spaces + 2;
}
for (j = i; j >= 1; j--)
{
if (j != n)
Console.Write(i);
}
Console.WriteLine();
}
spaces = spaces - 4;
for (i = 2; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write(i);
}
if (i != n)
{
for (k = 1; k <= spaces; k++)
{
Console.Write( " " );
}
spaces = spaces - 2;
}
for (j = i; j >= 1; j--)
{
if (j != n)
Console.Write(i);
}
Console.WriteLine();
}
}
public static void Main ()
{
pattern();
}
}
|
PHP
<?php
function pattern()
{
$i ; $j ; $k = 0;
$spaces = 1; $n = 7;
for ( $i = $n ; $i >= 1; $i --)
{
for ( $j = 1; $j <= $i ; $j ++)
{
echo $i ;
}
if ( $i != $n )
{
for ( $k = 1; $k <= $spaces ; $k ++)
{
echo " " ;
}
$spaces = $spaces + 2;
}
for ( $j = $i ; $j >= 1; $j --)
{
if ( $j != $n )
echo $i ;
}
echo "\n" ;
}
$spaces = $spaces - 4;
for ( $i = 2; $i <= $n ; $i ++)
{
for ( $j = 1; $j <= $i ; $j ++)
{
echo $i ;
}
if ( $i != $n )
{
for ( $k = 1; $k <= $spaces ; $k ++)
{
printf( " " );
}
$spaces = $spaces - 2;
}
for ( $j = $i ; $j >= 1; $j --)
{
if ( $j != $n )
echo $i ;
}
echo "\n" ;
}
}
pattern();
?>
|
Javascript
<script>
function pattern() {
var i,
j,
k = 0,
spaces = 1,
n = 7;
for (i = n; i >= 1; i--) {
for (j = 1; j <= i; j++) {
document.write(i);
}
if (i != n) {
for (k = 1; k <= spaces; k++) {
document.write( " " );
}
spaces = spaces + 2;
}
for (j = i; j >= 1; j--) {
if (j != n) document.write(i);
}
document.write( "<br>" );
}
spaces = spaces - 4;
for (i = 2; i <= n; i++) {
for (j = 1; j <= i; j++) {
document.write(i);
}
if (i != n) {
for (k = 1; k <= spaces; k++) {
document.write( " " );
}
spaces = spaces - 2;
}
for (j = i; j >= 1; j--) {
if (j != n) document.write(i);
}
document.write( "<br>" );
}
}
pattern();
</script>
|
Output :
7777777777777
666666 666666
55555 55555
4444 4444
333 333
22 22
1 1
22 22
333 333
4444 4444
55555 55555
666666 666666
7777777777777
Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...