Skip to content

Commit 479fbd9

Browse files
authored
Merge pull request #650 from vijayv18/number-patterns-java
Number patterns java
2 parents 644f1c1 + b301c78 commit 479fbd9

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class NumberPattern13 {
2+
3+
public static void main(String[] args) {
4+
int n=6,sum=0;
5+
for(int i=0;i<n;i++) {
6+
if(i%2==0) sum = 0;
7+
for(int j=0;j<=i;j++) {
8+
sum = Math.abs(sum - 1);
9+
System.out.print(sum+" ");
10+
}
11+
System.out.println();
12+
}
13+
}
14+
}
15+
16+
// Another method to solve this
17+
18+
/*
19+
20+
public class NumberPattern13 {
21+
22+
public static void main(String[] args) {
23+
int n=6;
24+
for(int i=0;i<n;i++) {
25+
for(int j=0;j<=i;j++) {
26+
if((i+j) % 2 == 0) {
27+
System.out.print(1+" ");
28+
}
29+
else {
30+
System.out.print(0+" ");
31+
}
32+
}
33+
System.out.println();
34+
}
35+
}
36+
}
37+
38+
*/
39+
40+
/*
41+
42+
The output for the above program
43+
44+
1
45+
0 1
46+
1 0 1
47+
0 1 0 1
48+
1 0 1 0 1
49+
0 1 0 1 0 1
50+
51+
52+
*/
53+
54+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class NumberPattern14 {
2+
3+
public static void main(String[] args) {
4+
int n = 4;
5+
int i, j;
6+
7+
// outer loop to handle number of rows
8+
for (i = 1; i <= n; i++) {
9+
// inner loop to print the spaces
10+
for (j = 1; j <= 2 * (n - i); j++) {
11+
System.out.print(" ");
12+
}
13+
14+
// inner loop to print the first part
15+
for (j = i; j >= 1; j--) {
16+
System.out.print(j + " ");
17+
}
18+
19+
// inner loop to print the second part
20+
for (j = 2; j <= i; j++) {
21+
System.out.print(j + " ");
22+
}
23+
24+
// printing new line for each row
25+
System.out.println();
26+
}
27+
}
28+
}
29+
30+
/*
31+
32+
The output for the above program
33+
34+
1
35+
2 1 2
36+
3 2 1 2 3
37+
4 3 2 1 2 3 4
38+
39+
*/
40+
41+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class NumberPattern15 {
2+
3+
public static void main(String[] args) {
4+
int n=4;
5+
for(int i=0;i<n;i++) {
6+
int count = i+1;
7+
for(int j=0;j<=i;j++) {
8+
System.out.print(" ");
9+
}
10+
for(int k=n-1;k>=i;k--) {
11+
System.out.print(count+" ");
12+
count++;
13+
}
14+
System.out.println();
15+
}
16+
}
17+
18+
}
19+
20+
/*
21+
22+
The output for the above program
23+
24+
1 2 3 4
25+
2 3 4
26+
3 4
27+
4
28+
29+
*/
30+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class NumberPattern16 {
2+
3+
public static void main(String[] args) {
4+
int n=4;
5+
for(int i=0;i<n;i++) {
6+
int count = i+1;
7+
for(int j=0;j<=i;j++) {
8+
System.out.print(" ");
9+
}
10+
for(int k=n-1;k>=i;k--) {
11+
System.out.print(count+" ");
12+
count++;
13+
}
14+
System.out.println();
15+
}
16+
for(int i=1;i<n;i++) {
17+
int count = n-i;
18+
for(int j=n-1;j>=i;j--) {
19+
System.out.print(" ");
20+
}
21+
for(int k=0;k<=i;k++) {
22+
System.out.print(count+" ");
23+
count++;
24+
}
25+
System.out.println();
26+
}
27+
}
28+
29+
}
30+
31+
/*
32+
33+
The output for the above program
34+
35+
1 2 3 4
36+
2 3 4
37+
3 4
38+
4
39+
3 4
40+
2 3 4
41+
1 2 3 4
42+
43+
*/
44+
45+

0 commit comments

Comments
 (0)