Program to print interesting pattern
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++
// CPP program to print pattern #include<bits/stdc++.h> using namespace std; void StarPattern(int height) { // This loop prints number of rows for (int i=0; i<height; i++ ) { // For every row, first '*' will be // printed Height-rowNumber times. for (int j = height-1; j>i; j--) { cout<<"*"; } // Print character '*' and Row number // alternately boolean variable to // decide wheather to print char or int bool printChar = false; for (int j = 0; j< ((i*2) +1); j++) { if ( printChar ) { cout<<"*"; } else { cout<< (i + 1); } // Each time after printing char // or int reverse the boolean variable // to print alternatively printChar = !printChar; } // After printing char and int, // it will print '*' for (int j = height-1; j>i; j--) { cout<<"*"; } cout<<endl; } } // driver code int main() { int height = 7; StarPattern(height); return 0; } // This code is contributed by Sahil_Bansall |
chevron_right
filter_none
Java
// Java program to print pattern import java.util.*; import java.lang.*; import java.io.*; public class GeeksforGeeks { public static void StarPattern(int height) { // This loop prints number of rows for (int i=0; i<height; i++ ) { // For every row, first '*' will be printed // Height-rowNumber times. for (int j = height-1; j>i; j--) { System.out.print("*"); } // Print character '*' and Row number alternately // boolean variable to decide wheather to print char or int boolean printChar = false; for (int j = 0; j< ((i*2) +1); j++) { if ( printChar ) { System.out.print("*"); } else { System.out.print(i + 1); } // Each time after printing char or int // reverse the boolean variable to print alternatively printChar = !printChar; } // After printing char and int, it will print '*' for (int j = height-1; j>i; j--) { System.out.print("*"); } System.out.println(); } } // Driver Code public static void main(String args[]) { int height = 7; StarPattern(height); } } |
chevron_right
filter_none
Python3
# Python program to print pattern def StarPattern(height): # This loop prints number of rows for i in range(height): # For every row, first '*' will be # printed Height-rowNumber times. for j in range(height-1,i,-1): print("*",end="") # Print character '*' and Row number # alternately boolean variable to # decide wheather to print char or int printChar = False for j in range(((i*2) +1)): if ( printChar ): print("*",end="") else: print(i + 1,end="") # Each time after printing char # or int reverse the boolean variable # to print alternatively printChar = not printChar # After printing char and int, # it will print '*' for j in range(height-1,i,-1): print("*",end="") print() # Driver code height = 7StarPattern(height) # This code is contributed # by Anant Agarwal. |
chevron_right
filter_none
C#
// C# program to print pattern using System; public class GeeksforGeeks { public static void StarPattern(int height) { // This loop prints number of rows for (int i = 0; i < height; i++) { // For every row, first '*' will be // printed Height-rowNumber times. for (int j = height - 1; j > i; j--) { Console.Write("*"); } // Print character '*' and Row number // alternately boolean variable to decide // wheather to print char or int bool printChar = false; for (int j = 0; j < ((i * 2) + 1); j++) { if (printChar) { Console.Write("*"); } else { Console.Write(i + 1); } // Each time after printing char // or int reverse the boolean variable // to print alternatively printChar = !printChar; } // After printing char and int, it will print '*' for (int j = height - 1; j > i; j--) { Console.Write("*"); } Console.WriteLine(); } } // Driver Code public static void Main() { int height = 7; StarPattern(height); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP implementation to // print pattern function StarPattern($height) { // This loop prints number // of rows for ($i = 0; $i < $height; $i++ ) { // For every row, first '*' // will be printed // Height-rowNumber times. for ($j = $height - 1; $j > $i; $j--) { echo "*"; } // Print character '*' and // Row number alternately // boolean variable to decide // whether to print char or int $printChar = false; for ($j = 0; $j< (($i * 2) + 1); $j++) { if ( $printChar ) { echo "*"; } else { echo ($i + 1); } // Each time after printing // char or int reverse the // boolean variable to print // alternatively $printChar = !$printChar; } // After printing char and int, // it will print '*' for ($j = $height-1; $j > $i; $j--) { echo "*"; } echo "\n"; } } // Driver code $height = 7; StarPattern($height); // This code is contributed by mits ?> |
chevron_right
filter_none
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**
Recommended Posts:
- Program to print the given Z Pattern
- Program to print the pattern "GFG"
- Program to print the pattern 'G'
- Program to print the given H Pattern
- Program to print the pattern ‘D’
- Program to print pattern
- Program to print Step Pattern
- Program to print a pattern of numbers
- Program to print numeric pattern | Set - 2
- Program to print Crown Pattern
- Program to print the arrow pattern
- Program to Print the Trapezium Pattern
- Program to print number pattern
- Program to print numeric pattern
- Program to print pyramid pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar



