Nested Loops Approach - O(n * m) Time and O(1) Space
The pattern can be printed using two nested loops.
The outer loop iterates through the rows (1 to n)
The inner loop iterates through the columns (1 to m)
A star (*) is printed for every cell. After printing all stars in a row, move to the next line to form the solid rectangle.
Total number stars printed is equal to n * m
C++
#include<bits/stdc++.h>usingnamespacestd;voidprintSolidRectangle(intn,intm){// Iterate through each row.for(inti=1;i<=n;i++){// Print m stars in the current row.for(intj=1;j<=m;j++){cout<<"* ";}// Move to the next line after printing each row.cout<<endl;}}intmain(){intn=3,m=5;printSolidRectangle(n,m);return0;}
Java
classGFG{staticvoidprintSolidRectangle(intn,intm){// Iterate through each row.for(inti=1;i<=n;i++){// Print m stars in the current row.for(intj=1;j<=m;j++){System.out.print("* ");}// Move to the next line after printing each// row.System.out.println();}}publicstaticvoidmain(String[]args){intn=3,m=5;printSolidRectangle(n,m);}}
Python
defprintSolidRectangle(n,m):# Iterate through each row.foriinrange(1,n+1):# Print m stars in the current row.forjinrange(1,m+1):print("* ",end="")# Move to the next line after printing each row.print()# Driver Codeif__name__=="__main__":n,m=3,5printSolidRectangle(n,m)
C#
usingSystem;classGFG{staticvoidprintSolidRectangle(intn,intm){// Iterate through each row.for(inti=1;i<=n;i++){// Print m stars in the current row.for(intj=1;j<=m;j++){Console.Write("* ");}// Move to the next line after printing each// row.Console.WriteLine();}}staticvoidMain(){intn=3,m=5;printSolidRectangle(n,m);}}
JavaScript
functionprintSolidRectangle(n,m){// Iterate through each row.for(leti=1;i<=n;i++){// Print m stars in the current row.for(letj=1;j<=m;j++){process.stdout.write("* ");}// Move to the next line after printing each row.console.log();}}// Driver Codeconstn=3,m=5;printSolidRectangle(n,m);