Program to print number with star pattern Last Updated : 17 Feb, 2023 Comments Improve Suggest changes Like Article Like Report We have to print the pattern as given in the below example.Examples : Input : 5 Output : 1 1*2 1*2*3 1*2 1 Input : 9 Output : 1 1*2 1*2*3 1*2*3*4 1*2*3*4*5 1*2*3*4 1*2*3 1*2 1 C++ #include <iostream> using namespace std; // C++ program to print above pattern void display(int n) { // 'sp' used for space and 'st' used for star int sp = n / 2, st = 1; // Outer for loop prints number of lines for (int i = 1; i <= n; i++) { for (int j = 1; j <= sp; j++) { cout << " "; } int count = 1; for (int k = 1; k <= st; k++) { if (k % 2 == 0) cout << "*"; else cout << count++; } cout << "\n"; if (i <= n / 2) { // Before reaching to half after // every line space is decreased // by 1 and star is increased by 2 sp = sp - 1; st = st + 2; } else { // After reaching to half // space is increased by 1 // and star is decreased by 2 sp = sp + 1; st = st - 2; } } } // Driver Code int main() { int n = 5; display(n); return 0; } // This code is contributed by vt_m Java // Java program to print above pattern import java.util.Scanner; class Pattern { void display(int n) { // 'sp' used for space and 'st' used for star int sp = n / 2, st = 1; // Outer for loop prints number of lines for (int i = 1; i <= n; i++) { for (int j = 1; j <= sp; j++) { System.out.print(" "); } int count = 1; for (int k = 1; k <= st; k++) { if (k % 2 == 0) System.out.print("*"); else System.out.print(count++); } System.out.println(); if (i <= n / 2) { // Before reaching to half after // every line space is decreased // by 1 and star is increased by 2 sp = sp - 1; st = st + 2; } else { // After reaching to half // space is increased by 1 // and star is decreased by 2 sp = sp + 1; st = st - 2; } } } // Driver Code public static void main(String[] args) { int n = 5; Pattern p = new Pattern(); p.display(n); } } Python3 # Python3 program to print above pattern def display(n): # 'sp' used for space and # 'st' used for star sp = n // 2 st = 1 # Outer for loop prints number # of lines for i in range(1, n + 1): for j in range(1, sp + 1): print(end = " ") count = 1 for k in range(1, st + 1): if (k % 2 == 0): print("*", end = "") else: print(count, end = "") count += 1 print() if (i <= n // 2): # Before reaching to half after # every line space is decreased # by 1 and star is increased by 2 sp = sp - 1 st = st + 2 else: # After reaching to half # space is increased by 1 # and star is decreased by 2 sp = sp + 1 st = st - 2 # Driver Code n = 5 display(n) # This code is contributed by # Mohit kumar 29 C# // C# program to print above pattern using System; class Pattern { void display(int n) { // 'sp' used for space and 'st' used for star int sp = n / 2, st = 1; // Outer for loop prints number of lines for (int i = 1; i <= n; i++) { for (int j = 1; j <= sp; j++) { Console.Write(" "); } int count = 1; for (int k = 1; k <= st; k++) { if (k % 2 == 0) Console.Write("*"); else Console.Write(count++); } Console.WriteLine(); if (i <= n / 2) { // Before reaching to half after // every line space is decreased // by 1 and star is increased by 2 sp = sp - 1; st = st + 2; } else { // After reaching to half // space is increased by 1 // and star is decreased by 2 sp = sp + 1; st = st - 2; } } } // Driver Code public static void Main() { int n = 5; Pattern p = new Pattern(); p.display(n); } } //This code is contributed by vt_m. PHP <?php // php program to print // above pattern function display($n) { // 'sp' used for space and // 'st' used for star $sp = $n / 2; $st = 1; // Outer for loop prints // number of lines for ($i = 1; $i <= $n; $i++) { for ($j = 1; $j <= $sp; $j++) { echo " "; } $count = 1; for ($k = 1; $k <= $st; $k++) { if ($k % 2 == 0) echo "*"; else echo $count++; } echo "\n"; if ($i <= $n / 2) { // Before reaching to half after // every line space is decreased // by 1 and star is increased by 2 $sp = $sp - 1; $st = $st + 2; } else { // After reaching to half // space is increased by 1 // and star is decreased by 2 $sp = $sp + 1; $st = $st - 2; } } } // Driver Code $n = 5; display($n); // This code is contributed by mits ?> JavaScript <script> // JavaScript program to print above pattern function display(n) { // 'sp' used for space and 'st' used for star var sp = n / 2, st = 1; // Outer for loop prints number of lines for (var i = 1; i <= n; i++) { for (var j = 1; j <= sp; j++) { document.write(" "); } var count = 1; for (var k = 1; k <= st; k++) { if (k % 2 == 0) document.write("*"); else document.write(count++); } document.write("<br>"); if (i <= n / 2) { // Before reaching to half after // every line space is decreased // by 1 and star is increased by 2 sp = sp - 1; st = st + 2; } else { // After reaching to half // space is increased by 1 // and star is decreased by 2 sp = sp + 1; st = st - 2; } } } // Driver Code var n = 5; display(n); </script> Output: 1 1*2 1*2*3 1*2 1 Time Complexity: O(n2), where n represents the given input.Auxiliary Space: O(1), no extra space is required, so it is a constant. Comment More infoAdvertise with us Next Article Program to print number with star pattern B bishaldubey Follow Improve Article Tags : Misc DSA Basic Coding Problems pattern-printing Practice Tags : Miscpattern-printing Similar Reads Program to print number pattern We have to print a pattern where in middle column contains only 1, right side columns contain constant digit which is greater than 1 and left side columns contains constant digit which is greater than 1. Every row should look like a Palindrome. Examples : Input : 3 Output : 1 2 1 2 1 Input : 5 Outpu 7 min read Program to print half diamond Number-Star pattern Given a number N which represents the number of rows. The task is to print a half diamond Number-Star pattern as shown in the below examples.Note: N is always an even number.Examples: Input: N = 4 Output: 2*2 1 1 2*2 Input: N = 6 Output: 3*3*3 2*2 1 1 2*2 3*3*3 On carefully observing the above patte 6 min read Program to Print a Pattern of Numbers The idea of pattern based programs is to understand the concept of nesting of for loops and how and where to place the alphabets / numbers / stars to make the desired pattern.Write to program to print the pattern of numbers in the following manner using for loop 1 232 34543 4567654567898765In almost 8 min read Program to print diamond pattern using numbers and stars Program to print the following pattern of a half diamond for N. Pattern for N = 4Example: Input: N = 5Output: 1 2*3 4*5*6 7*8*9*10 11*12*13*14*15 11*12*13*14*15 7*8*9*10 4*5*6 2*3 1 This program is divided into four parts. C++ // C++ program to print the half diamond // pattern using numbers and sta 5 min read Program to print half Diamond star pattern Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and 4 min read Like