JavaPattern Hack
JavaPattern Hack
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :
In this post, we will try to write the java programs to create pyramid of numbers in all different
patterns.
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
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
4
22
5
333
6
4444
7 333
8 22
9 1
10
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
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
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