FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
01-Core-Java/08-Java-Programs/054-Diamond-Pattern.java at main · shaikbasha-dev/01-Core-Java · GitHub
shaikbasha-dev
/
01-Core-Java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
01-Core-Java
/
08-Java-Programs
/
054-Diamond-Pattern.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
117 lines (86 loc) · 3.28 KB
Breadcrumbs
01-Core-Java
/
08-Java-Programs
/
054-Diamond-Pattern.java
Copy path
File metadata and controls
117 lines (86 loc) · 3.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* ============================================================================
* Program Name : Diamond Pattern
* File Name : 054-Diamond-Pattern.java
* Class Name : DiamondPattern
*
* Description:
* This program accepts the number of rows from the user and prints
* a Diamond Star Pattern using nested for loops.
*
* Objective:
* - Understand user input using the Scanner class.
* - Learn how to use nested for loops.
* - Print a Diamond Star Pattern.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import
java
.
util
.
Scanner
;
public
class
DiamondPattern
{
// The main() method is the entry point of every Java application.
public
static
void
main
(
String
[]
args
) {
// Create a Scanner object to read input from the keyboard.
Scanner
scanner
=
new
Scanner
(
System
.
in
);
// Ask the user to enter the number of rows.
System
.
out
.
print
(
"Enter the Number of Rows: "
);
// Read the number of rows entered by the user.
int
rows
=
scanner
.
nextInt
();
// Check whether the entered number of rows is valid.
if
(
rows
<=
0
) {
// Display an error message.
System
.
out
.
println
(
"Please enter a positive integer."
);
// Example Output:
// Please enter a positive integer.
}
else
{
// Display the heading.
System
.
out
.
println
(
"Diamond Pattern:"
);
// Print the upper half of the diamond.
for
(
int
i
=
1
;
i
<=
rows
;
i
++) {
// Print the required leading spaces.
for
(
int
j
=
1
;
j
<=
rows
-
i
;
j
++) {
// Print two spaces for proper alignment.
System
.
out
.
print
(
" "
);
}
// Print the stars for the current row.
for
(
int
k
=
1
;
k
<= (
2
*
i
-
1
);
k
++) {
// Print a star followed by a space.
System
.
out
.
print
(
"* "
);
}
// Move to the next line.
System
.
out
.
println
();
}
// Print the lower half of the diamond.
for
(
int
i
=
rows
-
1
;
i
>=
1
;
i
--) {
// Print the required leading spaces.
for
(
int
j
=
1
;
j
<=
rows
-
i
;
j
++) {
// Print two spaces for proper alignment.
System
.
out
.
print
(
" "
);
}
// Print the stars for the current row.
for
(
int
k
=
1
;
k
<= (
2
*
i
-
1
);
k
++) {
// Print a star followed by a space.
System
.
out
.
print
(
"* "
);
}
// Move to the next line.
System
.
out
.
println
();
}
// Example Output:
// Enter the Number of Rows: 5
// Diamond Pattern:
// *
// * * *
// * * * * *
// * * * * * * *
// * * * * * * * * *
// * * * * * * *
// * * * * *
// * * *
// *
}
// Close the Scanner object to release system resources.
scanner
.
close
();
}
}
Back
|
FazBrowse Home
|
New Git URL