1. Number Pattern - 1
class PrintPattern{
public static void main(String args[]){
int n = 5;
for ( int i = 1; i <= n; i++){
int j = n - i;
while (j > 0){
System.out.print(" ");
j--;
}
j = 1;
while (j <= i){
System.out.print(" " + j);
j++;
}
j = i - 1;
while (j > 0){
System.out.print(" " + j);
j--;
}
j = n - i;
while (j > 0){
System.out.print(" ");
j--;
}System.out.println();
}
}
}
Output :-
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
2. Number Pattern - 2
public class Patterns
{
public static void printNums(int n)
{
int i, j,num;
for(i=0; i<n; i++) // outer loop for rows
{
num=1;
for(j=0; j<=i; j++) // inner loop for rows
{
// printing num with a space
System.out.print(num+ " ");
//incrementing value of num
num++;
}
// ending line after each row
System.out.println();
}
}
public static void main(String args[])
{
int n = 6;
printNums(n);
}
}
Output :-
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
3. Number Pattern - 3
import java.util.Scanner;
public class Pattern
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
sc.close();
}
}
Output :-
Enter the number of rows:- 6
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
4. Number Pattern - 4
import java.util.Scanner;
public class Pattern
{
public static void main(String[] args) {
int i, j, k + 1;
for (i = 1; i <= 5; i++) {
for (j = 1; j < i + 1; j++) {
System.out.print(k++ + " ");
}
System.out.println();
}
}
}
Output :-
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
5. Number Pattern - 5
import java.util.Scanner;
public class Pattern
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = rows; i >1; i--)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
sc.close();
}
}
Output :-
Enter the number of rows :- 6
6
6 5
6 5 4
6 5 4 3
6 5 4 3 2
6 5 4 3 2 1
6. Number Pattern - 6
import java.util.Scanner;
public class Pattern
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.printin("Enter the number of rows");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) { for (int j=i; j>=1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
sc.close();
}
}
Output :-
Enter the number of rows :-6
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7. Number Pattern - 7
import java.util.Scanner;
public class Pattern
{
public static void main9String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
int number = 1;
System.out.printf("%' + (n - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.printf("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}
Output :-
11 11 2 11 3 3 11 4 6 4 1
8. Number Pattern - 8
import java.util.Scanner;
public class Pattern
{
public static void main(String[] args)
{
int n = 6;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= n; k++) {System.out.print(k+" "); } System.out.println(); } for (int i=n; i>=1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= n; k++)
{
System.out.print(k+" ");
}
System.out.println();
}
}
}
Output :-
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
5 6
4 5 6
3 4 5 6
2 3 4 5 6
1 2 3 4 5 6
9. Number Pattern - 9
import java.util.Scanner;
public class Pattern
{
public static void main(String[] args) {
for (int i = 1; i <= 4; i++)
{
int n = 4;
for (int j = 1; j <+ n - i; j++) {System.out.print(" "); } for (int k = i; k >= 1; k--)
{
System.out.print(k);
}
for (int l = 2; l <= i; l++) {System.out.print(l); } System.out.println(); } for (int i =3; i >= 1; i--)
{
int n = 3;
for (int j = 0; j<=n - i; j++) {System.out.print(" "); } for (int k = i; k>=1; k--)
{
System.out.print(k);
}
for (int l = 2; l <= i; l++)
{
System.out.print(l);
}
System.out.println();
}
}
}
Output :-
1
212
32123
4321234
32123
212
1
10. Number Pattern - 10
import java.util.Scanner;
public class Pattern
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = sc.nextlnt();
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
if(j%2 == 0)
{
System.out.print(0);
}
else
{
System.out.print(1);
}
}
System.out.println();
}
sc.close();
}
}
Output :-
Enter the number of rows :- 6
1
10
101
1010
10101
101010
11. Number Pattern - 11
import java.util.Scanner;
public class Pattern
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i<=rows; i++)
{
int num;
if(i%2 == 0)
{
num = 0;
for (int j = 1; j <= rows; j++)
{
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
else
{
num = 1;
for (int j=1; j<=rows; j++)
{
System.out.print(num);
num = (num == 0)? 1 : 0;
}
}
System.out.println();
}
sc.close();
}
}
Output :-
Enter the number of rows :- 6
101010
010101
101010
010101
101010
010101
0 Comments