Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
258 views

JavaPattern Hack

The document contains 9 patterns of numeric sequences generated using for loops in Java programs. Each pattern is presented along with the corresponding Java code that produces it. The code takes user input for the number of rows, then uses nested for loops and print statements to output the numbers in the desired pattern. Multiple patterns use variations of for loops to print the numbers in ascending, descending or both directions to create the visual patterns.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
258 views

JavaPattern Hack

The document contains 9 patterns of numeric sequences generated using for loops in Java programs. Each pattern is presented along with the corresponding Java code that produces it. The code takes user input for the number of rows, then uses nested for loops and print statements to output the numbers in the desired pattern. Multiple patterns use variations of for loops to print the numbers in ascending, descending or both directions to create the visual patterns.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Pattern 1 :

1
12
123
1234
12345
123456
1234567

Java Program :

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

How many rows you want in this pattern?


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

Pattern 2 :
1
22
333
4444
55555
666666
7777777

Java Program :

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

Output :

How many rows you want in this pattern?


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

Pattern 3 :
1
12
123
1234
12345
123456
1234567
123456
12345
1234
123
12
1

Java Program :

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

Java Program :

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

Output :

How many rows you want in this pattern?


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

Pattern 5 :
7654321
765432
76543
7654
765
76
7

Java Program :

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

Output :

How many rows you want in this pattern?


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

Pattern 6 :
7
76
765
7654
76543
765432
7654321

Java Program :

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

Java Program :

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

How many rows you want in this pattern?


7
Here is your pattern.!!!
7654321
654321
54321
4321
321
21
1
Pattern 8 :
1234567
123456
12345
1234
123
12
1
12
123
1234
12345
123456
1234567

Java Program :

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

Output :

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
121
12321
1234321
123454321
12345654321
1234567654321

Java Program :

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

Java Program :

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

Output :

How many rows you want in this pattern?


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

Pattern 11 :
?
1
2 1234567
3 234567
34567
4
4567
5 567
6 67
7 7
8 67
567
9 4567
10 34567
11 234567
12 1234567
13

Java Program :

1 import java.util.Scanner;
2
public class MainClass
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
//Taking rows value from the user
8
9
System.out.println("How many rows you want in this pattern?");
10
11 int rows = sc.nextInt();
12
13 System.out.println("Here is your pattern....!!!");
14
15 //Printing upper half of the pattern
16
17 for (int i = 1; i <= rows; i++)
18 {
//Printing i spaces at the beginning of each row
19
20 for (int j = 1; j < i; j++)
21 {
22 System.out.print(" ");
23 }
24
//Printing i to rows value at the end of each row
25
26 for (int j = i; j <= rows; j++)
27 {
28 System.out.print(j);
29 }
30
31 System.out.println();
}
32
33 //Printing lower half of the pattern
34
35 for (int i = rows-1; i >= 1; i--)
36 {
37 //Printing i spaces at the beginning of each row
38
39 for (int j = 1; j < i; j++)
{
40 System.out.print(" ");
41 }
42
43 //Printing i to rows value at the end of each row
44
45 for (int j = i; j <= rows; j++)
46 {
System.out.print(j);
47 }
48
49 System.out.println();
50 }
51
52 //Closing the resources
53
54 sc.close();
}
55 }
56
57
58
59
60
61
62
63

Output :

?
1
2 How many rows you want in this pattern?
3 7
4 Here is your pattern....!!!
5 1234567
234567
6 34567
7 4567
8 567
9 67
10 7
67
11 567
12 4567
13 34567
14 234567
1234567
15
16

Pattern 12 :
?
1
2 1234567
3 234567
4 34567
4567
5 567
6 67
7 7
8 67
567
9 4567
10 34567
11 234567
12 1234567
13

Java Program :

?
1 import java.util.Scanner;
2
3 public class MainClass
{
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
8 //Taking rows value from the user
9
10 System.out.println("How many rows you want in this pattern?");
11
12 int rows = sc.nextInt();
13
System.out.println("Here is your pattern....!!!");
14
15 //Printing upper half of the pattern
16
17 for (int i = 1; i <= rows; i++)
18 {
19 //Printing i spaces at the beginning of each row
20
21 for (int j = 1; j < i; j++)
{
22 System.out.print(" ");
23 }
24
25 //Printing i to rows value at the end of each row
26
27 for (int j = i; j <= rows; j++)
28 {
System.out.print(j+" ");
29 }
30
31 System.out.println();
32 }
33
34 //Printing lower half of the pattern
35
36 for (int i = rows-1; i >= 1; i--)
{
37 //Printing i spaces at the beginning of each row
38
39 for (int j = 1; j < i; j++)
40 {
41 System.out.print(" ");
42 }
43
//Printing i to rows value at the end of each row
44
45 for (int j = i; j <= rows; j++)
46 {
47 System.out.print(j+" ");
48 }
49
50 System.out.println();
}
51
52 //Closing the resources
53
54 sc.close();
55 }
56 }
57
58
59
60
61
62
63

Output :

?
1
2 How many rows you want in this pattern?
3 7
4 Here is your pattern....!!!
5 1234567
234567
6 34567
7 4567
8 567
9 67
10 7
67
11 567
12 4567
13 34567
14 234567
1234567
15
16

Pattern 13 :
1
10
101
1010
10101
101010
1010101

Java Program :

?
1
2
3 import java.util.Scanner;
4
5 public class MainClass
6 {
public static void main(String[] args)
7
{
8 Scanner sc = new Scanner(System.in);
9
10 System.out.println("How many rows you want in this pattern?");
11
12 int rows = sc.nextInt();
13
14 System.out.println("Here is your pattern....!!!");
15
16 for (int i = 1; i <= rows; i++)
{
17 for (int j = 1; j <= i; j++)
18 {
19 if(j%2 == 0)
20 {
System.out.print(0);
21 }
22 else
23 {
24 System.out.print(1);
25 }
}
26
27 System.out.println();
28 }
29
30 sc.close();
31 }
32 }
33
34

Output :

How many rows you want in this pattern?


7
Here is your pattern.!!!
1
10
101
1010
10101
101010
1010101

Pattern 14 :
1010101
0101010
1010101
0101010
1010101
0101010
1010101

Java Program :

?
import java.util.Scanner;
1
2 public class MainClass
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
System.out.println("How many rows you want in this pattern?");
8
9 int rows = sc.nextInt();
10
11 System.out.println("Here is your pattern....!!!");
12
13 for (int i = 1; i <= rows; i++)
14 {
15 int num;
16
if(i%2 == 0)
17 {
18 num = 0;
19
20 for (int j = 1; j <= rows; j++)
21 {
22 System.out.print(num);
23
num = (num == 0)? 1 : 0;
24 }
25 }
26 else
27 {
num = 1;
28
29 for (int j = 1; j <= rows; j++)
30 {
31 System.out.print(num);
32
33 num = (num == 0)? 1 : 0;
34 }
}
35
36 System.out.println();
37 }
38
39 sc.close();
40 }
}
41
42
43
44
45
46
47

Output :

How many rows you want in this pattern?


7
Here is your pattern.!!!
1010101
0101010
1010101
0101010
1010101
0101010
1010101
Pattern 15 :
1111111
1111122
1111333
1114444
1155555
1666666
7777777

Java Program :

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

Output :

How many rows you want in this pattern?


7
Here is your pattern.!!!
1111111
1111122
1111333
1114444
1155555
1666666
7777777

Pattern 16 :
0000000
0100000
0020000
0003000
0000400
0000050
0000006

Java Program :

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

Output :

How many rows you want in this pattern?


7
Here is your pattern.!!!
0000000
0100000
0020000
0003000
0000400
0000050
0000006
Pattern 17 :
1
26
3 7 10
4 8 11 13
5 9 12 14 15

Java Program :

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

How many rows you want in this pattern?


5
Here is your pattern.!!!
1
26
3 7 10
4 8 11 13
5 9 12 14 15
Pattern 18 :
Different Pyramid Pattern Programs In Java

In this post, we will try to write the java programs to create pyramid of numbers in all different
patterns.

Pattern 1 : Write java program


to create pyramid of numbers
like in Pattern1 of the above
image?

Take the input from the user and


assign it to noOfRows. This will
be the number of rows he wants
in a pyramid. Define one
variable called rowCount and
initialize it to 1. This will hold
the value of current row
count. At the beginning of each
row, we print i spaces
where i will be value
from noOfRows to 1. At the end of each row, we print rowCount value rowCount times. i.e in the
first row, 1 will be printed once. In the second row, 2 will be printed twice and so on. Below is the
java code which implements this logic.

1 import java.util.Scanner;
2
public class MainClass
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
8 //Taking noOfRows value from the user
9
System.out.println("How Many Rows You Want In Your Pyramid?");
10
11 int noOfRows = sc.nextInt();
12
13 //Initializing rowCount with 1
14
15 int rowCount = 1;
16
17 System.out.println("Here Is Your Pyramid");
18
//Implementing the logic
19
20 for (int i = noOfRows; i > 0; i--)
21 {
22 //Printing i spaces at the beginning of each row
23
24 for (int j = 1; j <= i; j++)
{
25 System.out.print(" ");
26 }
27
28 //Printing 'rowCount' value 'rowCount' times at the end of each row
29
30 for (int j = 1; j <= rowCount; j++)
31 {
System.out.print(rowCount+" ");
32 }
33
34 System.out.println();
35
36 //Incrementing the rowCount
37
38 rowCount++;
}
39 }
40 }
41
42
43
44
45
46

Output :

?
1 How Many Rows You Want In Your Pyramid?
2 9
Here Is Your Pyramid
3 1
4 22
5 333
6 4444
55555
7 666666
8 7777777
9 88888888
10 999999999
11
12

Pattern 2 : How to create pyramid of numbers in Java like in Pattern2 of the above image?

In this pattern also, we use same logic but instead of printing rowCount value rowCount times at
the end of each row, we print j where j value will be from 1 to rowCount.

1 import java.util.Scanner;
2
public class MainClass
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
//Taking noOfRows value from the user
8
9 System.out.println("How Many Rows You Want In Your Pyramid?");
10
11 int noOfRows = sc.nextInt();
12
13 //Initializing rowCount with 1
14
15 int rowCount = 1;
16
17 System.out.println("Here Is Your Pyramid");
18
//Implementing the logic
19
20 for (int i = noOfRows; i > 0; i--)
21 {
22 //Printing i spaces at the beginning of each row
23
24 for (int j = 1; j <= i; j++)
{
25 System.out.print(" ");
26 }
27
28 //Printing 'j' value at the end of each row
29
30 for (int j = 1; j <= rowCount; j++)
31 {
System.out.print(j+" ");
32 }
33
34 System.out.println();
35
36 //Incrementing the rowCount
37
38 rowCount++;
}
39 }
40 }
41
42
43
44
45
46

Output :

?
1
2 How Many Rows You Want In Your Pyramid?
3 9
Here Is Your Pyramid
4 1
5 12
6 123
7 1234
8 12345
123456
9 1234567
10 12345678
11 123456789
12

Pattern 3 : Write a java program to create pyramid of stars(*) like in the Pattern3 of the above
image?

The same logic is used here also. But, instead of printing rowCount or j value at the end of each
row, we print star(*).

?
import java.util.Scanner;
1
2 public class MainClass
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
//Taking noOfRows value from the user
8
9 System.out.println("How Many Rows You Want In Your Pyramid?");
10
11 int noOfRows = sc.nextInt();
12
13 //Initializing rowCount with 1
14
int rowCount = 1;
15
16 System.out.println("Here Is Your Pyramid");
17
18 //Implementing the logic
19
20 for (int i = noOfRows; i > 0; i--)
21 {
//Printing i spaces at the beginning of each row
22
23 for (int j = 1; j <= i; j++)
24 {
25 System.out.print(" ");
26 }
27
28 //Printing * at the end of each row
29
for (int j = 1; j <= rowCount; j++)
30 {
31 System.out.print("* ");
32 }
33
34 System.out.println();
35
//Incrementing the rowCount
36
37 rowCount++;
38 }
39 }
40 }
41
42
43
44
45
46

Output :

?
1 How Many Rows You Want In Your Pyramid?
2 9
Here Is Your Pyramid
3 *
4 * *
5 * * *
6 * * * *
7 * * * * *
* * * * * *
8 * * * * * * *
9 * * * * * * * *
10 * * * * * * * * *
11
12

Pattern 4 : Write java program to print pyramid of numbers like in the Pattern4 of the above
image?

In this problem, we print i*2 spaces at the beginning of each row instead of just i spaces. At the
end of each row, we print j where j value will be from 1 to rowCount and from rowCount-
1 to 1.

?
1 import java.util.Scanner;
2
3 public class MainClass
4 {
5 public static void main(String[] args)
{
6 Scanner sc = new Scanner(System.in);
7
8 //Taking noOfRows value from the user
9
10 System.out.println("How Many Rows You Want In Your Pyramid?");
11
12 int noOfRows = sc.nextInt();
13
//Initializing rowCount with 1
14
15 int rowCount = 1;
16
17 System.out.println("Here Is Your Pyramid");
18
19 //Implementing the logic
20
21 for (int i = noOfRows; i > 0; i--)
22 {
//Printing i*2 spaces at the beginning of each row
23
24 for (int j = 1; j <= i*2; j++)
25 {
26 System.out.print(" ");
27 }
28
//Printing j value where j value will be from 1 to rowCount
29
30
31 for (int j = 1; j <= rowCount; j++)
32 {
System.out.print(j+" ");
33 }
34
35 //Printing j value where j value will be from rowCount-1 to 1
36
37 for (int j = rowCount-1; j >= 1; j--)
38 {
System.out.print(j+" ");
39 }
40
41 System.out.println();
42
43 //Incrementing the rowCount
44
45 rowCount++;
46 }
}
47 }
48
49
50
51
52
53

Output :

?
1
2 How Many Rows You Want In Your Pyramid?
3 9
Here Is Your Pyramid
4 1
5 121
6 12321
7 1234321
8 123454321
12345654321
9 1234567654321
10 123456787654321
11 12345678987654321
12

Pattern 5 : Write Java program to print reverse pyramid of numbers like in the Pattern5 of the
above image?
In this problem, we iterate outer loop in the reverse order i.e from 1 to noOfRows and initialize
the rowCount tonoOfRows.

?
1 import java.util.Scanner;
2
3 public class MainClass
4 {
public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
8 //Taking noOfRows value from the user
9
10 System.out.println("How Many Rows You Want In Your Pyramid?");
11
12 int noOfRows = sc.nextInt();
13
//Initializing rowCount with noOfRows
14
15 int rowCount = noOfRows;
16
17 System.out.println("Here Is Your Pyramid");
18
19 //Implementing the logic
20
21 for (int i = 0; i < noOfRows; i++)
{
22 //Printing i*2 spaces at the beginning of each row
23
24 for (int j = 1; j <= i*2; j++)
25 {
26 System.out.print(" ");
27 }
28
//Printing j where j value will be from 1 to rowCount
29
30 for (int j = 1; j <= rowCount; j++)
31 {
32 System.out.print(j+" ");
33 }
34
//Printing j where j value will be from rowCount-1 to 1
35
36
for (int j = rowCount-1; j >= 1; j--)
37 {
38 System.out.print(j+" ");
39 }
40
41 System.out.println();
42
43 //Decrementing the rowCount
44
45 rowCount--;
}
46 }
47 }
48
49
50
51
52
53

Output :

?
1
2 How Many Rows You Want In Your Pyramid?
3 9
Here Is Your Pyramid
4 12345678987654321
5 123456787654321
6 1234567654321
7 12345654321
8 123454321
1234321
9 12321
10 121
11 1
12

Pattern 6 : How do you create pyramid of numbers like in the Pattern6 of the above image?

In this problem, at the end of each row we print j where j value will be
from i to noOfRows and from noOfRows-1to i.

?
1 import java.util.Scanner;
2
3 public class MainClass
4 {
public static void main(String[] args)
5 {
6 Scanner sc = new Scanner(System.in);
7
8 //Taking noOfRows value from the user
9
10 System.out.println("How Many Rows You Want In Your Pyramid?");
11
12 int noOfRows = sc.nextInt();
13
14 //Initializing rowCount with 1
15
int rowCount = 1;
16
17 System.out.println("Here Is Your Pyramid");
18
19 //Implementing the logic
20
21 for (int i = noOfRows; i >= 1; i--)
22 {
//Printing i*2 spaces at the beginning of each row
23
24 for (int j = 1; j <= i*2; j++)
25 {
26 System.out.print(" ");
27 }
28
29 //Printing j where j value will be from i to noOfRows
30
for (int j = i; j <= noOfRows; j++)
31 {
32 System.out.print(j+" ");
33 }
34
35 //Printing j where j value will be from noOfRows-1 to i
36
37 for (int j = noOfRows-1; j >= i; j--)
{
38 System.out.print(j+" ");
39 }
40
41 System.out.println();
42
43 //Incrementing the rowCount
44
rowCount++;
45 }
46 }
47 }
48
49
50
51
52
53

Output :

?
1
2 How Many Rows You Want In Your Pyramid?
3 9
Here Is Your Pyramid
4 9
5 898
6 78987
7 6789876
8 567898765
45678987654
9 3456789876543
10 234567898765432
11 12345678987654321
12

Pattern 19 : Diamond Pattern Programs In Java

Java Program To Print Diamond Of Numbers :


?

1 import java.util.Scanner;

2
3 public class MainClass

{
4
public static void main(String[] args)
5
{
6
Scanner sc = new Scanner(System.in);
7
8
//Taking noOfRows value from the user
9
10 System.out.println("How Many Rows You Want In Your Diamond?");
11
12 int noOfRows = sc.nextInt();

13
14 //Getting midRow of the diamond

15
16 int midRow = noOfRows/2;
17
18 //Initializing row with 1

19
20 int row = 1;

21
System.out.println("Here Is Your Diamond : ");
22
23
//Printing upper half of the diamond
24
25
for (int i = midRow; i > 0; i--)
26
{
27
//Printing i spaces at the beginning of each row
28
29 for (int j = 1; j <= i; j++)
30 {

31 System.out.print(" ");

32 }

33
//Printing row value j times at the end of each row
34
35
for (int j = 1; j <= row; j++)
36
{
37
System.out.print(row+" ");
38
}
39
40 System.out.println();
41
42 //Incrementing the row

43
44 row++;
45 }

46
47 //Printing lower half of the diamond

48
for (int i = 0; i <= midRow; i++)
49
{
50
//Printing i spaces at the beginning of each row
51
52
for (int j = 1; j <= i; j++)
53 {
54 System.out.print(" ");
55 }

56
57 //Printing row value j times at the end of each row

58
59 for (int j = row; j > 0; j--)

{
60
System.out.print(row+" ");
61
}
62
63
System.out.println();
64
65
//Decrementing the row
66
67 row--;
6 }

Output :?

1 How Many Rows You Want In Your Diamond?


2 7

3 Here Is Your Diamond :

1
4
22
5
333
6
4444
7 333
8 22

9 1

10

Pattern 20 : Floyds Triangle In Java

Refer this post for more info on Floyds Triangle.

Java Program To Print Floyds Triangle :


?
import java.util.Scanner;
1
2 public class FloydsTriangle
3 {
public static void main(String[] args)
4 {
5 System.out.println("How many rows you want in Floyd's Triangle?");
6
7 Scanner sc = new Scanner(System.in);
8
9 int noOfRows = sc.nextInt();
10
11 int value = 1;
12
System.out.println("Floyd's Triangle : ");
13
14 for (int i = 1; i <= noOfRows; i++)
15 {
16 for (int j = 1; j <= i; j++)
17 {
18 System.out.print(value+"\t");
19
value++;
20 }
21
22 System.out.println();
23 }
24 }
}
25
26
27
28
29

Output :

?
1 How many rows you want in Floyd's Triangle?
2 5
3 Floyd's Triangle :
4 1
5 2 3
6 4 5 6
7 7 8 9 10
8 11 12 13 14 15
How it works?
Let noOfRows = 5 and value = 1. for j = 3

1st iteration : As j > i

for i = 1 break inner for loop

for j = 1 Go To Next Line

Print value > Print 1 After 2nd iteration, output will look like
below,
value++ > value = 2
?
for j = 2 1 1
2 2 3
As j > i
3rd iteration :
break inner for loop
for i = 3
Go To Next Line
for j = 1
After 1st iteration, output will be,
Print value > Print 4
?
1 1 value++ > value = 5

2nd iteration : for j = 2

for i = 2 Print value > Print 5

for j = 1 value++ > value = 6

Print value > Print 2 for j = 3

value++ > value = 3 Print value > Print 6

for j = 2 value++ > value = 7

Print value > Print 3 for j = 4

value++ > value = 4 As j > i


break inner for loop As j > i

Go To Next Line break inner for loop

After 3rd iteration, output will be, Go To Next Line

? After 4th iteration, output will be,


1 1
2 2 3 ?
3 4 5 6 1 1
2 2 3
4th iteration : 3 4 5 6
4 7 8 9 10
for i = 4
5th iteration :
for j = 1
for i = 5
Print value > Print 7
for j = 1
value++ > value = 8
Print value > Print 11
for j = 2
value++ > value = 12
Print value > Print 8
for j = 2
value++ > value = 9
Print value > Print 12
for j = 3
value++ > value = 13
Print value > Print 9
for j = 3
value++ > value = 10
Print value > Print 13
for j = 4
value++ > value = 14
Print value > Print 10
for j = 4
value++ > value = 11
Print value > Print 14
for j = 5
value++ > value = 15
for j = 5 5 11 12 13 14 15

Print value > Print 15 6th Iteration :

value++ > value = 16 for i = 6

for j = 6 As i > noOfRows

As j > i Break outer for loop

break inner for loop Final Output :

Go To Next Line ?
1 How many rows you want in Floyd's Triangle?
2 5
After 5th iteration, output will be, Floyd's Triangle :
3
4 1
? 5 2 3
1 1 6 4 5 6
2 2 3 7 7 8 9 10
3 4 5 6 8 11 12 13 14 15
4 7 8 9 10

You might also like