PATTERN PROGRAMMING
ChristmasTree
import java.util.Scanner;
public class ChristmasTree {
public static final int height=5;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int width=sc.nextInt();
int space=width*5;
int x=1;
for(int a=1;a<=height;a++) {
for(int i=x;i<=width;i++) {
for(int j=space;j>=i;j--) {
System.out.print(" ");
}
for(int k=1;k<=i;k++) {
System.out.print("* ");
}
System.out.println();
}
x=x+2;
width=width+2;
}
for(int i=1;i<=4;i++) {
for(int j=space-3;j>=1;j--) {
System.out.print(" ");
}
for(int k=1;k<=4;k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
ContinuousNumberPattern
import java.util.Scanner;
public class ContinuousNumberPattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1
// 2 3
// 4 5 6
// 7 8 9 10
// 11 12 13 14 15
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
number(a);
}
static void number(int a) {
int num=1;
for(int row=0;row<a;row++) {
for(int j=0;j<=row;j++) {
System.out.print(num+" ");
num++;
}
System.out.println();
}
}
}
DegreeOfRotation180
import java.util.Scanner;
public class DegreeOfRotation180 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// *
// * *
// * * *
// * * * *
// * * * * *
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
pattern180(a);
}
static void pattern180(int a) {
for(int row=0;row<a;row++) {
for(int col=2*(a-row);col>=0;col--) {
System.out.print(" ");
}
for(int col=0;col<=row;col++) {
System.out.print("* ");
}
System.out.println();
}
}
}
DiamondPattern
import java.util.Scanner;
public class DiamondPattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
// *
// ***
// *****
// *******
// *********
// ***********
// *************
// ***********
// *********
// *******
// *****
// ***
// *
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
for(int i=0;i<=a;i++) {
for(int j=1;j<=a-i;j++)
System.out.print(" ");
for(int j=1;j<=2*i-1;j++)
System.out.print("*");
System.out.print("\n");
}
for(int i=a-1;i>=1;i--) {
for(int j=1;j<=a-i;j++)
System.out.print(" ");
for(int j=1;j<=2*i-1;j++)
System.out.print("*");
System.out.print("\n");
}
}
}
YOUTUBE PATTERNS 1
import java.util.Scanner;
public class KunalPatrtern17 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
pattern17(a);
}
static void pattern17(int a) {
for(int row=1;row<=2 * a;row++) {
int c=row>a ? 2*a-row:row;
for(int space=0;space<a-c;space++) {
System.out.print(" ");
}
for(int col=c;col>=1;col--) {
System.out.print(col+" ");
}
for(int col=2;col<=c;col++) {
System.out.print(col+" ");
}
System.out.println();
}
}
}
YOUTUBE PATTERNS 2
import java.util.Scanner;
public class KunalPattern1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
pattern1(a);
}
static void pattern1(int a) {
for(int row=1;row<=a;row++) {
for(int col=1;col<=a;col++) {
System.out.print("* ");
}
System.out.println();
}
}
}
YOUTUBE PATTERNS 3
import java.util.Scanner;
public class KunalPattern3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
pattern3(a);
}
static void pattern3(int a) {
for(int row=1;row<=a;row++) {
for(int col=1;col<=a-row+1;col++) {
System.out.print("* ");
}
System.out.println();
}
}
}
YOUTUBE PATTERNS 4
import java.util.Scanner;
public class KunalPattern4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
pattern4(a);
}
static void pattern4(int a) {
for(int row=1;row<=a;row++) {
for(int col=1;col<=row;col++) {
System.out.print(col+" ");
}
System.out.println();
}
}
}
YOUTUBE PATTERNS 5
import java.util.Scanner;
public class KunalPattern5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
pattern5(a);
}
static void pattern5(int a) {
for(int row=0;row<2 * a;row++) {
int c=row>a ? 2*a-row:row;
for(int col=0;col<c;col++) {
System.out.print("* ");
}
System.out.println();
}
}
}
YOUTUBE PATTERNS 6
import java.util.Scanner;
public class KunalPattern30 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
pattern30(a);
}
static void pattern30(int a) {
for(int row=1;row<=a;row++) {
for(int space=0;space<a-row;space++) {
System.out.print(" ");
}
for(int col=row;col>=1;col--) {
System.out.print(col+"");
}
for(int col=2;col<=row;col++) {
System.out.print(col+"");
}
System.out.println();
}
}
}
YOUTUBE PATTERNS 7
import java.util.Scanner;
public class KunalPattern31 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
pattern31(a);
}
static void pattern31(int a) {
int org=a;
a=2*a;
for(int row=0;row<=a;row++) {
for(int col=0;col<=a;col++) {
int atEveryIndex=org-Math.min(Math.min(row, col),Math.min(a-row, a-col));
System.out.print(atEveryIndex+" ");
}
System.out.println();
}
}
}
YOUTUBE PATTERNS 8
import java.util.Scanner;
public class KunnalPattern2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
pattern2(a);
}
static void pattern2(int a) {
for(int row=1;row<=a;row++) {
for(int col=1;col<=row;col++) {
System.out.print("* ");
}
//when one row is printed we need to add a new line
System.out.println();
}
}
}
MirrorPattern
import java.util.Scanner;
public class MirrorPattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1 2 3 4 5 6 7
// 2 3 4 5 6 7
// 3 4 5 6 7
// 4 5 6 7
// 5 6 7
// 6 7
// 7
// 6 7
// 5 6 7
// 4 5 6 7
// 3 4 5 6 7
// 2 3 4 5 6 7
// 1 2 3 4 5 6 7
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
mirror(a);
}
static void mirror(int a) {
for(int row=1;row<=a;row++) {
for(int col=1;col<row;col++) {
System.out.print(" ");
}
for(int col=row;col<=a;col++) {
System.out.print("* ");
}
System.out.println();
}
for(int row=a-1;row>=1;row--) {
for(int col=1;col<row;col++) {
System.out.print(" ");
}
for(int col=row;col<=a;col++) {
System.out.print("* ");
}
System.out.println();
}
}
}
NumberPattern
import java.util.Scanner;
public class NumberPattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1
// 1 2
// 1 2 3
// 1 2 3 4
// 1 2 3 4 5
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
number(a);
}
static void number(int a) {
for(int row=1;row<=a;row++) {
for(int col=1;col<=row;col++) {
System.out.print(col+" ");
}
System.out.println();
}
}
}
Patter1
import java.util.*;
class Main{
public static void main(String args[]) {
for(int i=7;i>=1;i--) {
for(int j=1;j<=2*i-1;j++) {
System.out.print(j+" ");
}
System.out.print("\n");
}
}
}
PyramidPattern
import java.util.Scanner;
public class PyramidPattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
// *
// * *
// * * *
// * * * *
// * * * * *
//Scanner sc=new Scanner(System.in);
//int a=sc.nextInt();
//for(int i=0;i<a;i++) {
// for(int j=a-i;j>1;j--) {
// System.out.print(" ");
// }
// for(int j=0;j<=i;j++) {
// System.out.print("* ");
// }
// System.out.println();
//}
//NUMBER BASED PATTERN
// 1
// 2 2
// 3 3 3
// 4 4 4 4
// 5 5 5 5 5
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int s,n,x;
s=a+4-1;
for(int i=1;i<=a;i++) {
for(x=s;x!=0;x--) {
System.out.print(" ");
}
for(int j=1;j<=i;j++) {
System.out.print(i+" ");
}
System.out.println();
s--;
}
}
}
ReverseTriangle
import java.util.Scanner;
public class ReverseTriangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1 2 3 4 5 6
// 2 3 4 5 6
// 3 4 5 6
// 4 5 6
// 5 6
// 6
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
reverseTriangle(a);
}
static void reverseTriangle(int a) {
for(int row=1;row<=a;row++) {
for(int col=1;col<row;col++) {
System.out.print(" ");
}
for(int col=row;col<=a;col++) {
System.out.print("* ");
}
System.out.println();
}
}
}
RightAngleTriangle
import java.util.Scanner;
public class RightAngleTriangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1
// 12
// 123
// 1234
// 12345
// 123456
// 1234567
// 12345678
// 123456789
// 12345678910
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
for(int i=1;i<=a;i++) {
for(int j=1;j<=i;j++) {
System.out.print(j);;
}
System.out.println(" ");
}
}
}
RightAngleTriangleWithNumberIncreasedBy1
import java.util.Scanner;
public class RightAngleTriangleWithNumberIncreasedBy1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1
// 2 3
// 4 5 6
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int k=1;
for(int i=1;i<=a;i++) {
for(int j=1;j<=i;j++) {
System.out.print(k+++" ");
}
System.out.println("");
}
}
}
RightAngleTriangleWithNumberWise
import java.util.Scanner;
public class RightAngleTriangleWithNumberWise {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1
// 22
// 333
// 4444
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
for(int i=1;i<=a;i++) {
for(int j=1;j<=i;j++) {
System.out.print(i);
}
System.out.println("");
}
}
}
SimplePattern
import java.util.Scanner;
public class SimplePattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
// *
// * *
// * * *
// * * * *
// * * * * *
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
pattern(a);
}
static void pattern(int a) {
for(int row=a-1;row>=0;row--) {
for(int col=0;col<=row;col++) {
System.out.print(col);
}
System.out.println();
}
}
}
TrianglePattern
import java.util.Scanner;
public class TrianglePattern {
public static void main(String[] args) {
// TODO Auto-generated method stub
// *
// * *
// * * *
// * * * *
// * * * * *
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
triangle(a);
}
static void triangle(int a) {
for(int row=0;row<a;row++) {
for(int col=a-row;col>1;col--) {
System.out.print(" ");
}
for(int col=0;col<=row;col++) {
System.out.print("* ");
}
System.out.println();
}
}
}