Pattern 1: Print nxn Star Square
Print a square pattern of stars (*) of size n x n.
Output
* * * ** * * ** * * ** * * *
Approach:
-
● Outer Loop (Rows): Run from
i = 0toi = n - 1 -
● Inner Loop (Columns): For each row, loop from
j = 0toj = n - 1 -
● Build Row String: Append
*in each inner loop iteration. - ● Print Row:After the inner loop, print the complete row string.
Time & Space Complexity:
Time Complexity:O(n^2)
Space Complexity:O(n)(temporary row string)
let n = 4;
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j < n; j++) {
row += "*";
}
console.log(row);
}
n = 4
for i in range(n):
row = ""
for j in range(n):
row += "*"
print(row)
public class StarSquare {
public static void main(String[] args) {
int n = 4;
for (int i = 0; i < n; i++) {
String row = "";
for (int j = 0; j < n; j++) {
row += "*";
}
System.out.println(row);
}
}
}
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j < n; j++) {
row += "*";
}
cout << row << endl;
}
return 0;
}
#include <stdio.h>
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
using System;
class Program {
static void Main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j < n; j++) {
row += "*";
}
Console.WriteLine(row);
}
}
}
Pattern 2: Right-Angled Star Triangle
Print a right-angled triangle of stars with n rows.
Output
** ** * ** * * *
Approach:
-
● Outer Loop (Rows): Run a loop from
i = 0toi = n - 1. Each iteration represents one row. -
● Inner Loop (Stars per Row): For each row
irun another loop fromj = 0toj = iand append a*character to a string. - ● Print Row:Print the string after the inner loop, completes for each row.
Time & Space Complexity:
Time Complexity:O(n^2)because the total number of stars printed is1+2+3+.......+n = n(n+1)/2.
Space Complexity:O(n)for the temporary string variable storing each row.
let n = 4;
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j <= i; j++) {
row += "*";
}
console.log(row);
}
n = 4
for i in range(n):
row = ""
for j in range(i + 1):
row += "*"
print(row)
public class StarTriangle {
public static void main(String[] args) {
int n = 4;
for (int i = 0; i < n; i++) {
String row = "";
for (int j = 0; j <= i; j++) {
row += "*";
}
System.out.println(row);
}
}
}
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j <= i; j++) {
row += "*";
}
cout << row << endl;
}
return 0;
}
#include <stdio.h>
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
using System;
class Program {
static void Main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j <= i; j++) {
row += "*";
}
Console.WriteLine(row);
}
}
}
Pattern 3: Print a Right-Angled Number Triangle
Write a program that prints a right-angled triangle of numbers of heightn.
Output
11 21 2 31 2 3 4
Approach:
-
● Outer loop (Rows): Run a loop from
i = 0toi < n.Each iteration represents a new row. -
● Inner loop (Numbers):Run an inner loop from
j = 0toj <= i, and appendj+1to the row. - ● Build and Print: Construct a string for the row and print it after the inner loop ends.
Time & Space Complexity:
Time Complexity: O(n^2)Each row can have up to nnumbers.
Space Complexity:O(n)Temporary string to build each row.
let n = 4;
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j <= i; j++) {
row += (j + 1);
}
console.log(row);
}
n = 4
for i in range(n):
row = ""
for j in range(i + 1):
row += str(j + 1)
print(row)
public class NumberTriangle {
public static void main(String[] args) {
int n = 4;
for (int i = 0; i < n; i++) {
String row = "";
for (int j = 0; j <= i; j++) {
row += (j + 1);
}
System.out.println(row);
}
}
}
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j <= i; j++) {
row += to_string(j + 1);
}
cout << row << endl;
}
return 0;
}
#include <stdio.h>
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
printf("%d", j + 1);
}
printf("\n");
}
return 0;
}
using System;
class Program {
static void Main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j <= i; j++) {
row += (j + 1);
}
Console.WriteLine(row);
}
}
}
Pattern 4: Print a Right-Angled Triangle of Repeated Numbers
Write a program that prints a right-angled triangle where each row contains the same number repeated.
Output
12 23 3 34 4 4 4
Approach:
-
● Outer loop (Rows): Loop from
i = 0toi < n. -
● Inner loop (Numbers):Loop from
j = 0toj <= i, appendingi+1as a string. - ● Build and Print: Build the row string and print it.
Time & Space Complexity:
Time Complexity: O(n^2)
Space Complexity:O(n)for the temporary row string.
let n = 4;
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j <= i; j++) {
row += (i + 1);
}
console.log(row);
}
n = 4
for i in range(n):
row = ""
for j in range(i + 1):
row += str(i + 1)
print(row)
public class NumberRepeatedTriangle {
public static void main(String[] args) {
int n = 4;
for (int i = 0; i < n; i++) {
String row = "";
for (int j = 0; j <= i; j++) {
row += (i + 1);
}
System.out.println(row);
}
}
}
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j <= i; j++) {
row += to_string(i + 1);
}
cout << row << endl;
}
return 0;
}
#include <stdio.h>
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
printf("%d", i + 1);
}
printf("\n");
}
return 0;
}
using System;
class Program {
static void Main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j <= i; j++) {
row += (i + 1).ToString();
}
Console.WriteLine(row);
}
}
}
Pattern 5: Print a Reverse Right-Angled Triangle of Increasing Numbers
Write a program that prints a reverse right-angled triangle where each row starts from 1 and the number of elements decreases with each row.
Output
1 2 3 41 2 31 21
Approach:
-
● Outer loop (Rows): Loop
ifrom0ton - 1.Each iteration represents a row. -
● Inner loop (Print Numbers):For each row, loop
jfrom0ton-i-1and appendj+1to a row string. - ● Print Row: After the inner loop, print the row string.
Time & Space Complexity:
Time Complexity: O(n^2)
Space Complexity:O(n)for the temporary row string.
let n = 4;
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j < n - i; j++) {
row += (j + 1);
}
console.log(row);
}
n = 4
for i in range(n):
row = ""
for j in range(n - i):
row += str(j + 1)
print(row)
public class ReverseNumberTriangle {
public static void main(String[] args) {
int n = 4;
for (int i = 0; i < n; i++) {
String row = "";
for (int j = 0; j < n - i; j++) {
row += (j + 1);
}
System.out.println(row);
}
}
}
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j < n - i; j++) {
row += to_string(j + 1);
}
cout << row << endl;
}
return 0;
}
#include <stdio.h>
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) {
printf("%d", j + 1);
}
printf("\n");
}
return 0;
}
using System;
class Program {
static void Main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j < n - i; j++) {
row += (j + 1).ToString();
}
Console.WriteLine(row);
}
}
}
Pattern 6: Print a Right-Aligned Right-Angled Triangle of Stars
Write a program that prints a right-aligned triangle of stars increasing row by row, with leading spaces for alignment.
Output
** ** * ** * * *
Approach:
-
● Outer loop (Rows): Loop
i = 0from0ton-1.Each iteration is a new row. -
● Inner loop 1(Spaces):For each row, add
n - i - 1spaces before the stars to right-align the triangle. -
● Inner loop 2(Stars):Add
i+1stars after the spaces. - ● Print Row: Combine the spaces and stars, then print the row.
Time & Space Complexity:
Time Complexity: O(n^2)
Space Complexity:O(n)for the row string.
let n = 4;
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j < n - (i + 1); j++) {
row += " ";
}
for (let k = 0; k < i + 1; k++) {
row += "*";
}
console.log(row);
}
n = 4
for i in range(n):
row = ""
for j in range(n - (i + 1)):
row += " "
for k in range(i + 1):
row += "*"
print(row)
public class RightAlignedTriangle {
public static void main(String[] args) {
int n = 4;
for (int i = 0; i < n; i++) {
String row = "";
for (int j = 0; j < n - (i + 1); j++) {
row += " ";
}
for (int k = 0; k < i + 1; k++) {
row += "*";
}
System.out.println(row);
}
}
}
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j < n - (i + 1); j++) {
row += " ";
}
for (int k = 0; k < i + 1; k++) {
row += "*";
}
cout << row << endl;
}
return 0;
}
#include <stdio.h>
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - (i + 1); j++) {
printf(" ");
}
for (int k = 0; k < i + 1; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
using System;
class Program {
static void Main(){
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j < n - (i + 1); j++) {
row += " ";
}
for (int k = 0; k < i + 1; k++) {
row += "*";
}
Console.WriteLine(row);
}
}
}
Pattern 7: Print a Right-Angled Triangle of Alternating 1s and 0s
Write a program that prints a triangle of alternating 1s and 0s starting with 1 on each row.
Output
11 01 0 11 0 1 0
Approach:
-
● Outer loop (Rows): Loop
ifrom0ton-1. -
● Initialize toggle = 1:Start each row with
toggle = 1.. -
● Inner loop (Columns):For each row, loop
jfrom0toi. On each iteration: - Append
toggleto the row string. - Flip
togglebetween 1 and 0. - ● Print Row: After inner loop, print the row string.
Time & Space Complexity:
Time Complexity: O(n^2)
Space Complexity:O(n)per row
let n = 4;
for (let i = 0; i < n; i++) {
let row = "";
let toggle = 1;
for (let j = 0; j < i + 1; j++) {
row += toggle;
toggle = toggle === 1 ? 0 : 1;
}
console.log(row);
}
n = 4
for i in range(n):
row = ""
toggle = 1
for j in range(i + 1):
row += str(toggle)
toggle = 0 if toggle == 1 else 1
print(row)
public class BinaryTriangle {
public static void main(String[] args) {
int n = 4;
for (int i = 0; i < n; i++) {
String row = "";
int toggle = 1;
for (int j = 0; j < i + 1; j++) {
row += toggle;
toggle = (toggle == 1) ? 0 : 1;
}
System.out.println(row);
}
}
}
#include <iostream>
using namespace std;
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
string row = "";
int toggle = 1;
for (int j = 0; j < i + 1; j++) {
row += to_string(toggle);
toggle = (toggle == 1) ? 0 : 1;
}
cout << row << endl;
}
return 0;
}
#include <stdio.h>
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
int toggle = 1;
for (int j = 0; j < i + 1; j++) {
printf("%d", toggle);
toggle = (toggle == 1) ? 0 : 1;
}
printf("\n");
}
return 0;
}
using System;
class Program {
static void Main() {
int n = 4;
for (int i = 0; i < n; i++) {
int toggle = 1;
for (int j = 0; j < i + 1; j++) {
Console.Write(toggle);
toggle = (toggle == 1) ? 0 : 1;
} Console.WriteLine();
}
}
}
Pattern 8: Right-Angled Triangle of Alternating 1s and 0s (Global Toggle)
Write a program to print a triangle of alternating 1s and 0s, but the toggle continues globally across rows.
Output
10 10 1 01 0 1 0
Approach:
-
● Global Toggle Variable: Declare
toggle = 1before the outer loop. -
● Outer Loop:Loop
ifrom0ton-1. -
● Inner loop:Loop
jfrom0toi.On each iteration: -
● Append:
toggleto the row string. -
● Flip:
toggle1 -> 0and0 -> 1. - ● Print the Row: after the inner loop.
Key Difference:
In the previous pattern, toggle = 1was reset each row. Here, the toggle continues globally across the entire pattern.
Time & Space Complexity:
Time Complexity: O(n^2)
Space Complexity:O(n)per row.
let n = 4;
let toggle = 1;
for (let i = 0; i < n; i++) {
let row = "";
for (let j = 0; j < i + 1; j++) {
row += toggle;
toggle = toggle === 1 ? 0 : 1;
}
console.log(row);
}
n = 4
toggle = 1
for i in range(n):
row = ""
for j in range(i + 1):
row += str(toggle)
toggle = 0 if toggle == 1 else 1
print(row)
public class GlobalToggleTriangle {
public static void main(String[] args) {
int n = 4;
int toggle = 1;
for (int i = 0; i < n; i++) {
String row = "";
for (int j = 0; j < i + 1; j++) {
row += toggle;
toggle = (toggle == 1) ? 0 : 1;
}
System.out.println(row);
}
}
}
#include <iostream>
using namespace std;
int main() {
int n = 4;
int toggle = 1;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j < i + 1; j++) {
row += to_string(toggle);
toggle = (toggle == 1) ? 0 : 1;
}
cout << row << endl;
}
return 0;
}
#include <stdio.h>
int main() {
int n = 4;
int toggle = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i + 1; j++) {
printf("%d", toggle);
toggle = (toggle == 1) ? 0 : 1;
}
printf("\n");
}
return 0;
}
using System;
class Program {
static void Main() {
int n = 4;
int toggle = 1;
for (int i = 0; i < n; i++) {
string row = "";
for (int j = 0; j < i + 1; j++) {
row += toggle.ToString();
toggle = (toggle == 1) ? 0 : 1;
} Console.WriteLine(row);
}
}
}
