0% found this document useful (0 votes)
4 views5 pages

PATTERNS

The document contains eight Java code snippets that demonstrate different patterns of asterisks and numbers printed in a console. Each code snippet uses nested loops to create various shapes, including rectangles, triangles, and more complex designs. The patterns vary in complexity and include spaces and numbers based on the row and column positions.

Uploaded by

Pavan kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

PATTERNS

The document contains eight Java code snippets that demonstrate different patterns of asterisks and numbers printed in a console. Each code snippet uses nested loops to create various shapes, including rectangles, triangles, and more complex designs. The patterns vary in complexity and include spaces and numbers based on the row and column positions.

Uploaded by

Pavan kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PATTERN:

1)

public class Main{


public static void main(String[] args) {
int n = 5;
for(int row=1;row<=n;row++){
for(int star=1;star<=n;star++){
System.out.print("*");
}
System.out.println();
}
}
}
2)

public class Main{


public static void main(String[] args) {
int n = 5;
for(int row=1;row<=n;row++){
for(int star=1;star<=row;star++){
System.out.print("*");
}
System.out.println();
}
}
}
3)
public class Main{
public static void main(String[] args) {
int n = 5;
for(int row=1;row<=n;row++){
for(int star=1;star<=row;star++){
System.out.print("*");
}
System.out.println();
}
}
}

4)

public class Main{


public static void main(String[] args) {
int n = 5;
for(int row=1;row<=n;row++){
for(int star=1;star<=(row*2)-1;star++){
System.out.print("*");
}
System.out.println();
}
}
}
5)
public class Main{
public static void main(String[] args) {
int n = 5;
for(int row=1;row<=n;row++){
for(int space = 1; space <=n-row; space++){
System.out.print(" ");
}
for(int star=1;star<=row;star++){
System.out.print("*");
}
System.out.println();
}
}
}
6)

public class Main{


public static void main(String[] args) {
int n = 5;
for(int row=1;row<=n;row++){
for(int space = 1; space <=row-1; space++){
System.out.print(" ");
}
for(int star=1;star<=n-row+1;star++){
System.out.print("*");
}
System.out.println();
}
}
}
7)
public class Main{
public static void main(String[] args) {
int n = 7;
for(int row=1;row<=n;row++){
if(row<=(n+1)/2) {
for (int star1 = 1; star1 <= row; star1++) {
System.out.print("*");
}
for (int space = 1; space <= n - (row * 2); space++) {
System.out.print(" ");
}
for (int star2 = 1; star2 <= row; star2++) {
System.out.print("*");
}
System.out.println();
}
else{
for (int star1 = 1; star1 <= n-row+1; star1++) {
System.out.print("*");

}
for (int space = 1; space <= (row*2)+n -15; space++) {
System.out.print(" ");
}
for (int star2 = 1; star2 <= n-row+1; star2++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
8)
public class Main{
public static void main(String[] args) {
int n = 5;
for(int row=1;row<=n;row++) {
for(int star=1;star<=n;star++){
if(row==star) {
if(row<=(n+1)/2){
System.out.print(n-row+1);
}
else{
System.out.print(row);
}
}
else if(row+star==n+1){
if(row<=(n+1)/2){
System.out.print(row);
}
else{
System.out.print(star);
}
}
else{
System.out.print(" ");
}
}
System.out.println();
}
}
}

You might also like