In this article, we will learn about printing square star pattern
Input: n = 5
Output:
* * * * * * * * * * * * * * * * * * * * * * * * *
Square Star Pattern:
Java Code:
import java.io.*; // Java code to demonstrate star Square public class GeeksForGeeks { // Function to demonstrate printing pattern public static void StarSquare(int n) { // outer loop to handle number of rows // n in this case for (int i = 1; i <= n; i++) { // inner loop to handle number of columns // values changing acc. to outer loop for (int j = 1; j <= n; j++) { // printing stars System.out.print("* "); } // end-line System.out.println(); } } // Main Function public static void main(String args[]) { int n = 5; StarRightTriangle(n); } }
C++ Code:
#include <iostream> using namespace std; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; col <= n; ++j) { cout << "* "; } cout << endl; } return 0; }
Python Code:
n = 5 # Loop through rows for i in range(n): # Loop to print pattern for j in range(n): print("*", end=' ') print()
Time Complexity: O(n*n) where n is given input
Space Complexity: O(1)
To Practice this Question on GFG
Other questions on patterns
Program to print right angle triangle using c++, java and python