What We Are Learn On This Post
Top 10 Common Number Pattern Programs For Java Interview
Write a Program to Print Below Pattern?
11111
12222
12333
12344
12345
package com.softwaretestingblog.InterviewPgms; public class NumberpatternPgm2 { public static void main(String[] args) { // TODO Auto-generated method stub int i,j; for(i=1;i<=5;i++) { for(j=1;j<=5;j++) { if(j<=i) System.out.print(j); else System.out.print(i); } System.out.println(""); } } }
Write a Program TO Print Below Structure?
package com.softwaretestingblog.programs; import java.util.Scanner; public class Pattern_Program1 { public static void main(String[] args) { // TODO Auto-generated method stub int num, space; System.out.print("Enter a number between 1 to 9 : "); Scanner reader = new Scanner(System.in); try { num = reader.nextInt(); space = num - 1; for (int i = 1; i <= num; i++) { for (space = 1; space <= (num - i); space++) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print(j); } for (int k = (i - 1); k >= 1; k--) { System.out.print(k); } System.out.println(); } } finally { reader.close(); } } }
Output:
Java Programs: Star Pyramid Patterns Program In Java
Write a Number Pattern Program On Below Structure?
1
22
333
4444
55555
Ans:
package com.softwaretestingblog.programs; import java.util.Scanner; public class Pattern_Program1 { public static void main(String[] args) { // TODO Auto-generated method stub int count = 5; for (int i = 1; i <= count; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); } } }
Output:
Read Also: Selenium Java Interview Questions
Write a Program to Below Pattern?
1
12
123
1234
12345
1234
123
12
1
Ans:
package com.softwaretestingblog.programs; import java.util.Scanner; public class Pattern_Program1 { public static void main(String[] args) { // TODO Auto-generated method stub int n = 5; for (int i = 1; i < n; i++) { for (int j = 1; j <= i; j++) System.out.print(j); System.out.println(); } for (int i = n; i >=0 ; i--) { for (int j = 1; j <= i; j++) System.out.print(j); System.out.println(); } System.out.println(); } }
Output:
Read Also: Finalize in Java
Write a Program For Below Pattern?
12345
1234
123
12
1
1
12
123
1234
12345
Ans:
package com.softwaretestingblog.programs; import java.util.Scanner; public class Pattern_Program1 { public static void main(String[] args) { // TODO Auto-generated method stub int n = 5; for (int i = n; i >= 0; i--) { for (int j = 1; j <= i; j++) System.out.print(j); System.out.println(); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) System.out.print(j); System.out.println(); } System.out.println(); } }
Output:
Write a Program For Below Patten?
1
01
101
0101
10101
Ans:
package com.softwaretestingblog.programs; import java.util.Scanner; public class Pattern_Program1 { public static void main(String[] args) { // TODO Auto-generated method stub int n, p, q; n = 5; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { p = 1; q = 0; } else { p = 0; q = 1; } for (int j = 1; j <= i; j++) if (j % 2 == 0) System.out.print(p); else System.out.print(q); System.out.println(); } } }
Output:
Write a Program for Below Patten?
1
23
456
78910
Ans:
package com.softwaretestingblog.programs; import java.util.Scanner; public class Pattern_Program1 { public static void main(String[] args) { // TODO Auto-generated method stub int rows, k = 1; rows = 4; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) System.out.print(k++); System.out.println(); } } }
Output:
Write a Program for Below Patten?
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Ans:
package com.selenium.actions; public class Pattern_Program { public static void main(String[] args) { int n= 5; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } for (int k = 1; k <= i; k++) { System.out.print(k + " "); } System.out.println(""); } } }
Output:
Write a Program for Below Patten?
1****
12***
123**
1234*
12345
Ans:
package com.softwaretestingblog.programs; import java.util.Scanner; public class Pattern_Program1 { public static void main(String[] args) { // TODO Auto-generated method stub int i, j, k; int n = 5; for (i = 1; i <= n; i++) { for (j = 1; j <= i; ++j) System.out.print(j); for (k = n - i; k >= 1; k--) System.out.print("*"); System.out.println(); } } }
Output:
Write a Program for Below Patten?
How many rows do you want in this pattern?
5
Here is your pattern….!!!
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Ans:
package com.softwaretestingblog.programs; import java.util.Scanner; public class Pattern_Program1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); 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++) { int num = i; for (int j = 1; j <= i; j++) { System.out.print(num+" "); num = num+rows-j; } System.out.println(); } sc.close(); } }
Output:
Write a Program for Below Number Pattern Programs?
How many rows do you want in this pattern?
5
Here is your pattern….!!!
11111
11122
11333
14444
55555
Ans:
package com.softwaretestingblog.programs; import java.util.Scanner; public class Pattern_Program1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); 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 <= rows-i; j++) { System.out.print(1); } for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); } sc.close(); } }
Output:
Write a Program to Print Below Number Pattern Programs
Ans:
package com.softwaretestingblog.programs; import java.util.Scanner; public class Myclass { public static void main(String arg[]) { Scanner sc =new Scanner(System.in); System.out.println("Enter the number"); int n=sc.nextInt(); for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { System.out.print(" "+j +" "); } for(int k=1;k<i;k++) { System.out.print(" "+k+" "); } System.out.println(); } } }
Write a Program To Print Below Number Pattern Programs?
———When Row No = 3———————
1112
3222
3334
———When Row No = 5———————
111112
322222
333334
544444
555556
Ans:
package com.softwaretestingblog.InterviewPgms; public class NumberpatternPgm1 { public static void numberPattern(int rows) { for (int row = 0; row < rows; row++) { for (int col = 0; col < rows + 1; col++) { int number = 0; if (row % 2 == 0) { number = col < rows ? row + 1 : row + 2; } else { number = col == 0 ? row + 2 : row + 1; } System.out.print(number); } System.out.println(); } } public static void main(String[] args) { // TODO Auto-generated method stub NumberpatternPgm1 obj=new NumberpatternPgm1(); System.out.println("---------When Row No = 3---------------------"); obj.numberPattern(3); System.out.println("---------When Row No = 5---------------------"); obj.numberPattern(5); } }
Leave a Reply