Given a number N which denotes the number of rows, the task is to follow the below pattern to print the first N rows of it.
Pattern:
*
*#
*#*
*#*#
*#*#*
Examples:
Input: N = 2
Output:
*
*#Input: N = 6
Output:
*
*#
*#*
*#*#
*#*#*
*#*#*#
Approach: Follow the below steps to implement the above pattern:
- Initialize two variables row and col to 1. These will be used to keep track of the current row and column that we are on in the pattern.
- Use a loop to iterate the row from 1 to N. This will be the outer loop and will represent each row of the pattern.
- Inside the outer loop, use another loop to iterate col from 1 to row. This will be the inner loop and will represent each column in the current row.
- Inside the inner loop, check if col is even or odd.
- If it is even, print a “#” character. If it is odd, print a “*” character.
- After the inner loop has been completed, move to the next line (this will start a new row in the pattern).
- Inside the outer loop, use another loop to iterate col from 1 to row. This will be the inner loop and will represent each column in the current row.
- After the outer loop has been completed, the pattern has been printed.
Below is the implementation of the above approach:
C++
// C++ code for the above approach:#include <bits/stdc++.h>using namespace std;int main(){ int n = 6; // Loop through each row of // the pattern for (int row = 1; row <= n; row++) { // Loop through each column of // the pattern for (int col = 1; col <= row; col++) { // If the column number is even, // print a "#" character if (col % 2 == 0) { cout << "#"; } // If the column number is odd, // print a "*" character else { cout << "*"; } } // Move to the next line after // printing each row cout << endl; } return 0;} |
Python3
# Python code for the above approach:n = 6# Loop through each row of# the patternfor row in range(1, n+1): # Loop through each column of # the pattern for col in range(1, row+1): # If the column number is even, # print a "#" character if col % 2 == 0: print("#", end="") # If the column number is odd, # print a "*" character else: print("*", end="") # Move to the next line after # printing each row print() |
C#
// C# code for the above approach:using System;public class Gfg{ public static void Main(string[] args) { int n = 6; // Loop through each row of // the pattern for (int row = 1; row <= n; row++) { // Loop through each column of // the pattern for (int col = 1; col <= row; col++) { // If the column number is even, // print a "#" character if (col % 2 == 0) { Console.Write("#"); } // If the column number is odd, // print a "*" character else { Console.Write("*"); } } // Move to the next line after // printing each row Console.Write("\n"); } }}// This code is contributed by ritaagarwal. |
Javascript
// Javascript code for the above approach:let n = 6;// Loop through each row of// the patternfor (let row = 1; row <= n; row++) { // Loop through each column of // the pattern for (let col = 1; col <= row; col++) { // If the column number is even, // print a "#" character if (col % 2 == 0) { console.log("#"); } // If the column number is odd, // print a "*" character else { console.log("*"); } } // Move to the next line after // printing each row console.log("<br>");}// This code is contributed by poojaagarwal2. |
Java
// Java program for the above approachimport java.util.*;class GFG{ public static void main(String args[]) { int n = 6; // Loop through each row of // the pattern for (int row = 1; row <= n; row++) { // Loop through each column of // the pattern for (int col = 1; col <= row; col++) { // If the column number is even, // print a "#" character if (col % 2 == 0) { System.out.print("#"); } // If the column number is odd, // print a "*" character else { System.out.print("*"); } } // Move to the next line after // printing each row System.out.print("\n"); } } } |
* *# *#* *#*# *#*#* *#*#*#
Time Complexity: O(N2) // since two nested loops are used the time taken by the algorithm to complete all operations is quadratic.
Auxiliary Space: O(1) //since there is a basic arithmetic operation that takes constant time.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
