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

Java Pattern

The document contains 10 different patterns that can be created using Java programs. For each pattern, it provides the output of the pattern for a given number of rows, as well as the Java code used to generate that pattern. The patterns include numeric and alphanumeric sequences that are printed in various formats, such as ascending, descending, diagonal, triangular, etc. The code demonstrates the use of for loops and printing to systematically generate and output the different patterns.

Uploaded by

Kuldeep Dwivedi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views

Java Pattern

The document contains 10 different patterns that can be created using Java programs. For each pattern, it provides the output of the pattern for a given number of rows, as well as the Java code used to generate that pattern. The patterns include numeric and alphanumeric sequences that are printed in various formats, such as ascending, descending, diagonal, triangular, etc. The code demonstrates the use of for loops and printing to systematically generate and output the different patterns.

Uploaded by

Kuldeep Dwivedi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Pattern 1 :

1
1 2
1 23
1 234
1 2345
1 23456
1 234567

Java Program :

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

System.out.println("Here is your pattern....!!!");

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


{
for (int j = 1; j <= i; j++)
{
System.out.print(j+&quot; &quot;);
}

System.out.println();
}

//Close the resources

sc.close();
}
}

Output :

How many rows you want in this pattern?


7
Here is your pattern….!!!
1
12
123
1234
12345
123456
1234567

Pattern 2 :
1
2 2
3 33
4 444
5 5555
6 66666
7 777777

Java Program :

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println(&quot;How many rows you want in this pattern?&quot;);

int rows = sc.nextInt();

System.out.println(&quot;Here is your pattern....!!!&quot;);

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


{
for (int j = 1; j <= i; j++)
{
System.out.print(i+&quot; &quot;);
}

System.out.println();
}

//Close the resources

sc.close();
}
}

Output :

How many rows you want in this pattern?


7
Here is your pattern….!!!
1
22
333
4444
55555
666666
7777777

READ : How To Find All The Leaders In An Integer Array?

Pattern 3 :
1
1 2
1 23
1 234
1 2345
1 23456
1 234567
1 23456
1 2345
1 234
1 23
1 2
1

Java Program :

1. import java.util.Scanner;
2.
3. public class MainClass
4. {
5. public static void main(String[] args)
6. {
7. Scanner sc = new Scanner(System.in);
8.
9. //Taking rows value from the user
10.
11. System.out.println(&quot;How many rows you want in this pattern?&quot;);
12.
13. int rows = sc.nextInt();
14.
15. System.out.println(&quot;Here is your pattern....!!!&quot;);
16.
17. //Printing upper half of the pattern
18.
19. for (int i = 1; i <= rows; i++)
20. {
21. for (int j = 1; j <= i; j++)
22. {
23. System.out.print(j+&quot; &quot;);
24. }
25.
26. System.out.println();
27. }
28.
29. //Printing lower half of the pattern
30.
31. for (int i = rows-1; i >= 1; i--)
32. {
33. for (int j = 1; j <= i; j++)
34. {
35. System.out.print(j+&quot; &quot;);
36. }
37.
38. System.out.println();
39. }
40.
41. //Closing the resources
42.
43. sc.close();
44. }
45. }

Output :

How many rows you want in this pattern?


7
Here is your pattern….!!!
1
12
123
1234
12345
123456
1234567
123456
12345
1234
123
12
1

Pattern 4 :
1 234567
1 23456
1 2345
1 234
1 23
1 2
1

Java Program :

1. import java.util.Scanner;
2.
3. public class MainClass
4. {
5. public static void main(String[] args)
6. {
7. Scanner sc = new Scanner(System.in);
8.
9. //Taking rows value from the user
10.
11. System.out.println(&quot;How many rows you want in this pattern?&quot;);
12.
13. int rows = sc.nextInt();
14.
15. System.out.println(&quot;Here is your pattern....!!!&quot;);
16.
17. for (int i = rows; i >= 1; i--)
18. {
19. for (int j = 1; j <= i; j++)
20. {
21. System.out.print(j+&quot; &quot;);
22. }
23.
24. System.out.println();
25. }
26.
27. //Closing the resources
28.
29. sc.close();
30. }
31. }

Output :

How many rows you want in this pattern?


7
Here is your pattern….!!!
1234567
123456
12345
1234
123
12
1

[quads id=5]

Pattern 5 :
7 654321
7 65432
7 6543
7 654
7 65
7 6
7

Java Program :

1. import java.util.Scanner;
2.
3. public class MainClass
4. {
5. public static void main(String[] args)
6. {
7. Scanner sc = new Scanner(System.in);
8.
9. //Taking rows value from the user
10.
11. System.out.println(&quot;How many rows you want in this pattern?&quot;);
12.
13. int rows = sc.nextInt();
14.
15. System.out.println(&quot;Here is your pattern....!!!&quot;);
16.
17. for (int i = 1; i <= rows; i++)
18. {
19. for (int j = rows; j >= i; j--)
20. {
21. System.out.print(j+&quot; &quot;);
22. }
23.
24. System.out.println();
25. }
26.
27. //Closing the resources
28.
29. sc.close();
30. }
31. }

Output :

READ : How To Find Duplicate Characters In A String In Java?

How many rows you want in this pattern?


7
Here is your pattern….!!!
7654321
765432
76543
7654
765
76
7

Pattern 6 :
7
7 6
7 65
7 654
7 6543
7 65432
7 654321

Java Program :

1. import java.util.Scanner;
2.
3. public class MainClass
4. {
5. public static void main(String[] args)
6. {
7. Scanner sc = new Scanner(System.in);
8.
9. //Taking rows value from the user
10.
11. System.out.println(&quot;How many rows you want in this pattern?&quot;);
12.
13. int rows = sc.nextInt();
14.
15. System.out.println(&quot;Here is your pattern....!!!&quot;);
16.
17. for (int i = rows; i >= 1; i--)
18. {
19. for (int j = rows; j >= i; j--)
20. {
21. System.out.print(j+&quot; &quot;);
22. }
23.
24. System.out.println();
25. }
26.
27. //Closing the resources
28.
29. sc.close();
30. }
31. }

Output :

How many rows you want in this pattern?


7
Here is your pattern….!!!
7
76
765
7654
76543
765432
7654321

Pattern 7 :
7 654321
6 54321
5 4321
4 321
3 21
2 1
1

Java Program :

1. import java.util.Scanner;
2.
3. public class MainClass
4. {
5. public static void main(String[] args)
6. {
7. Scanner sc = new Scanner(System.in);
8.
9. //Taking rows value from the user
10.
11. System.out.println(&quot;How many rows you want in this pattern?&quot;);
12.
13. int rows = sc.nextInt();
14.
15. System.out.println(&quot;Here is your pattern....!!!&quot;);
16.
17. for (int i = rows; i >= 1; i--)
18. {
19. for (int j = i; j >= 1; j--)
20. {
21. System.out.print(j+&quot; &quot;);
22. }
23.
24. System.out.println();
25. }
26.
27. //Closing the resources
28.
29. sc.close();
30. }
31. }
Output :

How many rows you want in this pattern?


7
Here is your pattern….!!!
7654321
654321
54321
4321
321
21
1

Pattern 8 :
1 234567
1 23456
1 2345
1 234
1 23
1 2
1
1 2
1 23
1 234
1 2345
1 23456
1 234567

Java Program :

1. import java.util.Scanner;
2.
3. public class MainClass
4. {
5. public static void main(String[] args)
6. {
7. Scanner sc = new Scanner(System.in);
8.
9. //Taking rows value from the user
10.
11. System.out.println(&quot;How many rows you want in this pattern?&quot;);
12.
13. int rows = sc.nextInt();
14.
15. System.out.println(&quot;Here is your pattern....!!!&quot;);
16.
17. //Printing upper half of the pattern
18.
19. for (int i = rows; i >= 1; i--)
20. {
21. for (int j = 1; j <= i; j++)
22. {
23. System.out.print(j+&quot; &quot;);
24. }
25.
26. System.out.println();
27. }
28.
29. //Printing lower half of the pattern
30.
31. for (int i = 2; i <= rows; i++)
32. {
33. for (int j = 1; j <= i; j++)
34. {
35. System.out.print(j+&quot; &quot;);
36. }
37.
38. System.out.println();
39. }
40.
41. //Closing the resources
42.
43. sc.close();
44. }
45. }

Output :

READ : How To Find Longest Substring Without Repeating Characters In Java?

How many rows you want in this pattern?


7
Here is your pattern….!!!
1234567
123456
12345
1234
123
12
1
12
123
1234
12345
123456
1234567

Pattern 9 :
1
1 21
1 2321
1 2343 21
1 2345 4321
1 2345 654321
1 2345 67654321

Java Program :

1. import java.util.Scanner;
2.
3. public class MainClass
4. {
5. public static void main(String[] args)
6. {
7. Scanner sc = new Scanner(System.in);
8.
9. //Taking rows value from the user
10.
11. System.out.println(&quot;How many rows you want in this pattern?&quot;);
12.
13. int rows = sc.nextInt();
14.
15. System.out.println(&quot;Here is your pattern....!!!&quot;);
16.
17. for (int i = 1; i <= rows; i++)
18. {
19. //Printing first half of the row
20.
21. for (int j = 1; j <= i; j++)
22. {
23. System.out.print(j+&quot; &quot;);
24. }
25.
26. //Printing second half of the row
27.
28. for (int j = i-1; j >= 1; j--)
29. {
30. System.out.print(j+&quot; &quot;);
31. }
32.
33. System.out.println();
34. }
35.
36. //Closing the resources
37.
38. sc.close();
39. }
40. }

Output :

How many rows you want in this pattern?


7
Here is your pattern….!!!
1
121
12321
1234321
123454321
12345654321
1234567654321

Pattern 10 :
1
2 1
3 21
4 321
5 4321
6 54321
7 654321

Java Program :

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println(&quot;How many rows you want in this pattern?&quot;);

int rows = sc.nextInt();

System.out.println(&quot;Here is your pattern....!!!&quot;);

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


{
for (int j = i; j >= 1; j--)
{
System.out.print(j+&quot; &quot;);
}

System.out.println();
}

//Close the resources

sc.close();
}
}

Output :

How many rows you want in this pattern?


7
Here is your pattern….!!!
1
21
321
4321
54321
654321
7654321

You might also like