0% found this document useful (0 votes)
24 views28 pages

Pattern Programming 1 1 Lyst9038 Lyst9468

The document contains 21 code snippets that print various patterns using for loops in Java. Each snippet contains a for loop that iterates to print the desired pattern. The patterns include stars, numbers, and empty spaces arranged in triangle, square, and diagonal shapes.

Uploaded by

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

Pattern Programming 1 1 Lyst9038 Lyst9468

The document contains 21 code snippets that print various patterns using for loops in Java. Each snippet contains a for loop that iterates to print the desired pattern. The patterns include stars, numbers, and empty spaces arranged in triangle, square, and diagonal shapes.

Uploaded by

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

1.

Print below pattern


*
public class Demo {

public static void main(String[] args) {

System.out.println("*");

2.Print below pattern


*****
public class Demo {

public static void main(String[] args) {

for(int i = 1; i <= 5; i++)


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

3.Print below pattern


*
*
*
*
*
public class Demo {

public static void main(String[] args) {

for(int i = 1; i <= 5; i++)


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

4.Print below pattern


*****
*****
*****
*****
*****
public class Demo {

public static void main(String[] args) {

for(int i = 1; i <= 5; i++)


{
for(int j = 1; j<= 5; j++)
{
System.out.print("*");
}
System.out.println();
}

}
5.Print below pattern
11111
22222
33333
44444
55555
public class Demo {

public static void main(String[] args) {

for(int i = 1; i <= 5; i++)


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

6.Print below pattern


12345
12345
12345
12345
12345
public class Demo {

public static void main(String[] args) {

for(int i = 1; i <= 5; i++)


{
for(int j = 1; j<= 5; j++)
{
System.out.print(j + " ");
}
System.out.println();
}

7.Print below pattern


*****
* *
* *
* *
*****
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i == 0 || i == n-1 || j == 0 || j == n-1)
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}

8.Print below pattern


01 02 03 04 05
06 07 08 09 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
public class Demo {

public static void main(String[] args) {

int n = 5;
int count = 1;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(count < 10)
{
System.out.print("0");
}
System.out.print(count + " ");
count++;
}
System.out.println();
}

9.Print below pattern


01 02 03 04 05
02 04 06 08 10
03 06 09 12 15
04 08 12 16 20
05 10 15 20 25
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if( i*j < 10)
{
System.out.print("0");
}
System.out.print(i*j + " ");
}
System.out.println();
}

}
10. Print below pattern
12345
23456
34567
45678
56789
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print(i+j+1 + " ");
}
System.out.println();
}

11. Print below pattern


*
**
***
****
*****
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}

12. Print below pattern


1
12
123
1234
12345
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
System.out.print(j+1 + " ");
}
System.out.println();
}

13. Print below pattern


1
22
333
4444
55555
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
System.out.print(i+1 + " ");
}
System.out.println();
}

}
14. Print below pattern
*
**
***
****
*****
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int k = 0; k < n-1-i; k++)
{
System.out.print(" ");
}
for(int j = 0; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}

}
15. Print below pattern
*
* *
* * *
* * * *
* * * * *
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int k = 0; k < n-1-i; k++)
{
System.out.print(" ");
}
for(int j = 0; j <= i; j++)
{
System.out.print("*" + " ");
}
System.out.println();
}

}
16. Print below pattern
*
* *
* *
* *
* * * * *
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int k = 0; k < n-1-i; k++)
{
System.out.print(" ");
}
for(int j = 0; j <= i; j++)
{
if( j == 0 || j == i || i == n-1)
{
System.out.print("*" + " ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}

}
17. Print below pattern
1
1 2
1 3
1 4
1 2 3 4 5
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int k = 0; k < n-1-i; k++)
{
System.out.print(" ");
}
for(int j = 0; j <= i; j++)
{
if( j == 0 || j == i || i == n-1)
{
System.out.print(j+1 + " ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}

}
18. Print below pattern
1
12
1 3
1 4
12345
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
if( j == 0 || j == i || i == n-1)
{
System.out.print(j+1 + " ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}

}
19. Print below pattern
1
12
123
1234
12345
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
System.out.print(j+1 + " ");
}
System.out.println();
}

20. Print below pattern


12345
1234
123
12
1
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-i; j++)
{
System.out.print(j+1 + " ");
}
System.out.println();
}

21. Print below pattern


12345
2345
345
45
5
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-i; j++)
{
System.out.print(i+j+1 + " ");
}
System.out.println();
}

22. Print below pattern


12345
2 5
3 5
4 5
5
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-i; j++)
{
if(i == 0 || i == n-1 || j == 0 || j == n-i-1)
{
System.out.print(i+j+1 + " ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}

}
23. Print below pattern
*
***
*****
*******
*********
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= (2*i)-1; j++)
{
System.out.print("*");
}
System.out.println();
}

24. Print below pattern


1
123
12345
1234567
123456789
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= (2*i)-1; j++)
{
System.out.print(j);
}
System.out.println();
}

25. Print below pattern


*
***
*****
*******
*********
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 1; i <= n; i++)
{
for(int k = 1; k <= n-i; k++)
{
System.out.print(" ");
}
for(int j = 1; j <= (2*i)-1; j++)
{
System.out.print("* ");
}
System.out.println();
}

26. Print below pattern


1
121
12321
1234321
123454321
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 1; i <= n; i++)
{
int count = 1;
for(int j = 1; j <= (2*i)-1; j++)
{
if(j < i)
{
System.out.print(count++ + " ");
}
else
{
System.out.print(count-- + " ");
}
}
System.out.println();
}

27. Print below pattern


1
121
12321
1234321
123454321
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 1; i <= n; i++)
{
int count = 1;
for(int k = 1; k <= n-i; k++)
{
System.out.print(" ");
}
for(int j = 1; j <= (2*i)-1; j++)
{
if(j < i)
{
System.out.print(count++ + " ");
}
else
{
System.out.print(count-- + " ");
}
}
System.out.println();
}

28. Print below pattern


111112
222223
333334
544444
555556
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 1; i <= n; i++)
{
if(i%2 == 0)
{
System.out.print(i+1);
}
for(int j = 1; j <= n; j++)
{
System.out.print(i);
}
if(i%2 != 0)
{
System.out.print(i+1);
}
System.out.println();
}

}
29. Print below pattern
1
22
333
4444
55555
4444
333
22
1
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
System.out.print(i+1 + " ");
}

System.out.println();
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-i; j++)
{
System.out.print(n-i + " ");
}
System.out.println();
}

}
30. Print below pattern
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
public class Demo {

public static void main(String[] args) {

int n = 5;
int count = 1;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
System.out.print(count++ + " ");
}

System.out.println();
}

31. Print below pattern


1
3* 2
6* 5* 4
10 * 9 * 8 * 7
15 * 14 * 13 * 12 * 11
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 1; i <= n; i++)
{
int count = (i*(i+1))/2;
for(int j = 1; j <= i; j++)
{
System.out.print(count-- + " ");
if(j < i)
{
System.out.print("* ");
}
}
System.out.println();
}

32. Print below pattern


A
BB
CCC
DDDD
EEEEE
public class Demo {

public static void main(String[] args) {

int n = 5;
char ch = 'A';
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(ch + " ");
}
System.out.println();
ch++;
}

33. Print below pattern


A
AB
ABC
ABCD
ABCDE
public class Demo {

public static void main(String[] args) {

int n = 5;
for(int i = 1; i <= n; i++)
{
char ch = 'A';
for(int j = 1; j <= i; j++)
{
System.out.print(ch + " ");
ch++;
}
System.out.println();
}
}
}
34. Print below pattern
A
BC
DEF
GHIJ
KLMNO
public class Demo {

public static void main(String[] args) {

int n = 5;
char ch = 'A';
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(ch + " ");
ch++;
}
System.out.println();
}

35. Print below pattern


A
CB
DEF
JIHG
KLMNO
public class Demo {

public static void main(String[] args) {

int n = 5;
char ch = 'A';
for(int i = 1; i <= n; i++)
{
if(i%2 == 0)
{
char chRev = (char)(ch + i - 1);
for(int j = 1; j <= i; j++)
{
System.out.print(chRev-- + " ");
ch++;
}
System.out.println();
}
else
{
for(int j = 1; j <= i; j++)
{
System.out.print(ch + " ");
ch++;
}
System.out.println();
}
}
}
}

You might also like