-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava Program to Print Square Star Pattern
More file actions
56 lines (45 loc) · 1.14 KB
/
Copy pathJava Program to Print Square Star Pattern
File metadata and controls
56 lines (45 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Java Program to Print Square Pattern
// Case 1: Hollow rectangle
// Importing input output classes
import java.io.*;
// Main class
class GFG {
// Method 1
// To print hollow rectangle
static void print_rectangle(int k, int l)
{
int a, b;
// Nested for loops for iterations
// Outer loop for rows
for (a = 1; a <= k; a++) {
// Inner loop for columns
for (b = 1; b <= l; b++) {
// Condition check using logical OR operator
// over rows and columns positions
// if found at circumference of rectangle
if (a == 1 || a == k || b == 1 || b == l)
// Print the star pattern
System.out.print("*");
else
// Rest inside square print the empty
// spaces
System.out.print(" ");
}
// By now we are done with only 1 row so
// New line
System.out.println();
}
}
// Method 2
// Main driver method
public static void main(String args[])
{
// Declaring and initializing rows and columns
// For square row = columns
// Custom input initialization values
int rows = 8, columns = 22;
// Calling the method1 to print hollow rectangle
// pattern
print_rectangle(rows, columns);
}
}