0% found this document useful (0 votes)
23 views

Pattern Questions

The document contains code for printing several patterns in Java including a rectangle with borders of asterisks, an inverted triangle of asterisks with increasing numbers of spaces, a triangle of alternating 0s and 1s, an inverted half-pyramid of increasing numbers, and a butterfly pattern with increasing then decreasing rows of asterisks separated by spaces. The code uses for loops and conditionals to control the printing of asterisks, numbers, spaces and newlines to create the different shapes.

Uploaded by

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

Pattern Questions

The document contains code for printing several patterns in Java including a rectangle with borders of asterisks, an inverted triangle of asterisks with increasing numbers of spaces, a triangle of alternating 0s and 1s, an inverted half-pyramid of increasing numbers, and a butterfly pattern with increasing then decreasing rows of asterisks separated by spaces. The code uses for loops and conditionals to control the printing of asterisks, numbers, spaces and newlines to create the different shapes.

Uploaded by

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

import java.util.

*;

public class Main{

public static void main(String[] args){

Scanner sc= new Scanner(System.in);

System.out.print("Enter Rows: ");

int row = sc.nextInt();

System.out.print("Enter Columns: ");

int columns = sc.nextInt();


int line = row;

for(int i=1; i<=line; i++){

for(int j = 1; j<=columns;j++){

if(i==1|| j==1 || i==row|| j==columns){

System.out.print("* ");
}
else{
System.out.print(" ");
}
}

System.out.println();
}

// Printing inverted star triangle:-


import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter line ");

int line = sc.nextInt();

for (int i = 1; i <= line; i++) {

for (int gap = 0; gap < line - i; gap++) {

System.out.print(" "); }
for (int star = 1; star <= i; star++) {

System.out.print("* ");
}
System.out.println();
}

// Printing inverted star triangle:-


import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter line ");

int line = sc.nextInt();

for (int i = 1; i <= line; i++) {

for (int gap = 0; gap < line - i; gap++) {

System.out.print(" "); }

for (int star = 1; star <= i; star++) {

System.out.print("* ");
}
System.out.println();
}

// INVERTED HALF-PYRAMID with Numbers


import java.util.*;

public class Main {

public static void main(String [] agrs){

Scanner sc = new Scanner(System.in);


int line = sc.nextInt();

for(int i= 1 ; i<= line; i++){

for(int j= 1 ; j<= line+1-i ; j++){

System.out.print(" " +j);


}
System.out.println();
}

}
}

// TRIANGLE PATTERN WITH 0 AND 1


import java.util.*;

public class Main {

public static void main(String [] agrs) {

Scanner sc = new Scanner(System.in);

int line = sc.nextInt();

for(int i= 1; i<=line; i++){

for (int j =1 ; j <=i ; j ++){

if((i+j)%2== 0){

System.out.print("1");

}
else{
System.out.print("0");
}
}
System.out.println("");

}}

// BUTTERFLY PATTERN
import java.util.*;

public class Main {

public static void main(String [] agrs) {

Scanner sc = new Scanner(System.in);

int line = sc.nextInt();


for (int i = 1; i <= line; i++) {

for (int star = 1; star <= i; star++) {

System.out.print("* ");
}

for (int gap = 1; gap <= 2 * (line - i); gap++) {


System.out.print(" ");
}
for (int star = 1; star <= i; star++) {

System.out.print("* ");

}
System.out.println();

for (int i = line; i >= 1; i--) {

for (int star = 1; star <= i; star++) {

System.out.print("* ");
}

for (int gap = 1; gap <= 2 * (line - i); gap++) {


System.out.print(" ");
}
for (int star = 1; star <= i; star++) {

System.out.print("* ");

}
System.out.println();

} }

You might also like