Program to print following pattern:
********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
****5*5*5*5*5****
***6*6*6*6*6*6***
**7*7*7*7*7*7*7**
Examples :
Input : 4
Output :
********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
Input :5
Output :
********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
****5*5*5*5*5****
C++
#include<bits/stdc++.h>
using namespace std;
void StarPattern( int height)
{
for ( int i=0; i<height; i++ )
{
for ( int j = height-1; j>i; j--)
{
cout<< "*" ;
}
bool printChar = false ;
for ( int j = 0; j< ((i*2) +1); j++)
{
if ( printChar )
{
cout<< "*" ;
}
else
{
cout<< (i + 1);
}
printChar = !printChar;
}
for ( int j = height-1; j>i; j--)
{
cout<< "*" ;
}
cout<<endl;
}
}
int main()
{
int height = 7;
StarPattern(height);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
public class GeeksforGeeks
{
public static void StarPattern( int height)
{
for ( int i= 0 ; i<height; i++ )
{
for ( int j = height- 1 ; j>i; j--)
{
System.out.print( "*" );
}
boolean printChar = false ;
for ( int j = 0 ; j< ((i* 2 ) + 1 ); j++)
{
if ( printChar )
{
System.out.print( "*" );
}
else
{
System.out.print(i + 1 );
}
printChar = !printChar;
}
for ( int j = height- 1 ; j>i; j--)
{
System.out.print( "*" );
}
System.out.println();
}
}
public static void main(String args[])
{
int height = 7 ;
StarPattern(height);
}
}
|
Python3
def StarPattern(height):
for i in range (height):
for j in range (height - 1 ,i, - 1 ):
print ( "*" ,end = "")
printChar = False
for j in range (((i * 2 ) + 1 )):
if ( printChar ):
print ( "*" ,end = "")
else :
print (i + 1 ,end = "")
printChar = not printChar
for j in range (height - 1 ,i, - 1 ):
print ( "*" ,end = "")
print ()
height = 7
StarPattern(height)
|
C#
using System;
public class GeeksforGeeks {
public static void StarPattern( int height)
{
for ( int i = 0; i < height; i++) {
for ( int j = height - 1; j > i; j--) {
Console.Write( "*" );
}
bool printChar = false ;
for ( int j = 0; j < ((i * 2) + 1); j++) {
if (printChar) {
Console.Write( "*" );
}
else {
Console.Write(i + 1);
}
printChar = !printChar;
}
for ( int j = height - 1; j > i; j--) {
Console.Write( "*" );
}
Console.WriteLine();
}
}
public static void Main()
{
int height = 7;
StarPattern(height);
}
}
|
PHP
<?php
function StarPattern( $height )
{
for ( $i = 0; $i < $height ; $i ++ )
{
for ( $j = $height - 1; $j > $i ;
$j --)
{
echo "*" ;
}
$printChar = false;
for ( $j = 0; $j < (( $i * 2) + 1);
$j ++)
{
if ( $printChar )
{
echo "*" ;
}
else
{
echo ( $i + 1);
}
$printChar = ! $printChar ;
}
for ( $j = $height -1; $j > $i ;
$j --)
{
echo "*" ;
}
echo "\n" ;
}
}
$height = 7;
StarPattern( $height );
?>
|
Javascript
<script>
function StarPattern(height)
{
for ( var i = 0; i < height; i++)
{
for ( var j = height + 1; j > i; j--)
{
document.write( "*" );
}
var printChar = false ;
for ( var j = 0; j < i * 2 + 1; j++)
{
if (printChar) {
document.write( "*" );
} else {
document.write(i + 1);
}
printChar = !printChar;
}
for ( var j = height + 1; j > i; j--) {
document.write( "*" );
}
document.write( "<br>" );
}
}
var height = 7;
StarPattern(height);
</script>
|
Output :
********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
****5*5*5*5*5****
***6*6*6*6*6*6***
**7*7*7*7*7*7*7**
Time complexity: O(h2) where h is the height.
Auxiliary Space: O(1)
Method 2(Using conditions):
We can use the positions of the pattern to identify the places to print the number or ‘*’.
For example take height = 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 * * * * * * * * 1 * * * * * * * *
2 * * * * * * * 2 * 2 * * * * * * *
3 * * * * * * 3 * 3 * 3 * * * * * *
4 * * * * * 4 * 4 * 4 * 4 * * * * *
5 * * * * 5 * 5 * 5 * 5 * 5 * * * *
Now imagine that we are printing a box filled ‘*’ and at specific places we see numbers. Notice their positions and observe that in all lines it starts from i+j == 10 and ends at j-i <= height+4. And it is printing only when i+j is even. There we will print i when it is i+j is even and in the above-specified range.
Java
import java.io.*;
public class GFGInterestingPattern {
public static void main(String[] args)
{
int height = 7 ;
printPattern(height);
}
public static void printPattern( int height)
{
for ( int i = 1 ; i <= height; i++) {
for ( int j = 1 ; j <= 17 ; j++) {
if ((i + j) % 2 == 0 && i + j >= 10
&& j - i <= 8 ) {
System.out.print(i);
}
else {
System.out.print( '*' );
}
}
System.out.println();
}
}
}
|
Python3
def print_pattern(height):
for i in range ( 1 , height + 1 ):
for j in range ( 1 , 18 ):
if (i + j) % 2 = = 0 and i + j > = 10 and j - i < = 8 :
print (i, end = "")
else :
print ( "*" , end = "")
print ()
def main():
height = 7
print_pattern(height)
main()
|
Javascript
function print_pattern(height) {
for (i = 1; i <= height; i++) { temp= "" ;
for (j = 1; j <= 18; j++) {
if ((i + j) % 2 == 0 && i + j >= 10 && j - i <= 8) {
temp =temp + i;
} else {
temp =temp + "*" ;
}
}
console.log(temp);
}
}
function main() {
height = 7;
print_pattern(height);
}
main();
|
C++
#include <iostream>
using namespace std;
void printPattern( int height)
{
for ( int i = 1; i <= height; i++) {
for ( int j = 1; j <= 17; j++) {
if ((i + j) % 2 == 0 && i + j >= 10
&& j - i <= 8) {
cout << i;
}
else {
cout << "*" ;
}
}
cout << endl;
}
}
int main()
{
int height = 7;
printPattern(height);
return 0;
}
|
C#
using System;
class GFGInterestingPattern {
public static void Main( string [] args)
{
int height = 7;
printPattern(height);
}
public static void printPattern( int height)
{
for ( int i = 1; i <= height; i++) {
for ( int j = 1; j <= 17; j++) {
if ((i + j) % 2 == 0 && i + j >= 10
&& j - i <= 8) {
Console.Write(i);
}
else {
Console.Write( '*' );
}
}
Console.WriteLine();
}
}
}
|
Output********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
****5*5*5*5*5****
***6*6*6*6*6*6***
**7*7*7*7*7*7*7**
Time complexity: O(h2) where h is the height.
Auxiliary Space: O(1)