0% found this document useful (0 votes)
58 views27 pages

Number Patterns

The document contains 12 Java programs that print out different number patterns. Each program contains a main method that takes user input for the number of rows, then uses nested for loops to print the pattern. The output for each program demonstrates the pattern it produces.

Uploaded by

MAHESH V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views27 pages

Number Patterns

The document contains 12 Java programs that print out different number patterns. Each program contains a main method that takes user input for the number of rows, then uses nested for loops to print the pattern. The output for each program demonstrates the pattern it produces.

Uploaded by

MAHESH V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Number Pattern Programs In Java

Pattern 1:
package [Link];

import [Link];

public class Pattern1


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();


[Link]("** Printing the pattern... **");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
[Link](j + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Pattern 2:
package [Link];

import [Link];

public class Pattern2


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
[Link](i + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Pattern 3:
package [Link];

import [Link];

public class Pattern3


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
[Link](j + " ");
}
[Link]();
}
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
[Link](j + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Pattern 4:
package [Link];

import [Link];

public class Pattern4


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = rows; i >= 1; i--)


{
for (int j = 1; j <= i; j++)
{
[Link](j + " ");
}
[Link]();
}

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
[Link](j + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Pattern 5:
package [Link];

import [Link];

public class Pattern5


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = rows; i >= 1; i--)


{
for (int j = i; j >= 1; j--)
{
[Link](j + " ");
}
[Link]();
}

for (int i = 1; i <= rows; i++)


{
for (int j = i; j >= 1; j--)
{
[Link](j + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Pattern 6:
package [Link];

import [Link];

public class Pattern6


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = rows; j > i; j--)
{
[Link](" ");
}
for (int k = 1; k <= i; k++)
{
[Link](k + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Pattern 7:
package [Link];

import [Link];

public class Pattern7


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = rows; j >= i; j--)
{
[Link](j + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Pattern 8:
package [Link];
import [Link];

public class Pattern8


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = rows; i >= 1; i--)


{
for (int j = rows; j >= i; j--)
{
[Link](j + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Pattern 9:
package [Link];

import [Link];

public class Pattern9


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");


for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
[Link](j + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Pattern 10:
package [Link];

import [Link];

public class Pattern10


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();


int k = 1;

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
[Link](k + " ");
k++;
}
[Link]();
}
}
}
Output
Enter the number of rows to print the pattern
5
** Printing the pattern... **
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Pattern 11:
package [Link];

import [Link];

public class Pattern11


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = i; j >= 1; j--)
{
[Link](j + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Pattern 12:
package [Link];

import [Link];

public class Pattern12


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
int temp = i;
for (int j = i; j >= 1; j--)
{
[Link](temp + " ");
temp = temp + rows;
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
2 7
3 8 13
4 9 14 19
5 10 15 20 25
Pattern 13:
package [Link];

import [Link];

public class Pattern13


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");


for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
[Link](" ");
}

int temp = 1;
for (int k = 1; k <= i; k++)
{
[Link](temp + " ");
temp = temp * (i - k) / (k);
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Pattern 14:
package [Link];

import [Link];

public class Pattern14


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
[Link](j + " ");
}
for (int k = i - 1; k >= 1; k--)
{
[Link](k + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
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
Pattern 15:
package [Link];

import [Link];

public class Pattern15


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j < i; j++)
{
[Link](" ");
}
for (int k = 1; k <= rows - i + 1; k++)
{
[Link](k + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Pattern 16:
package [Link];

import [Link];

public class Pattern16


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = rows; j > i; j--)
{
[Link](" ");
}

for (int k = 1; k <= i; k++)


{
[Link](k + " ");
}
[Link]();
}
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
[Link](" ");
}
for (int k = 1; k <= rows - i; k++)
{
[Link](k + " ");
}
[Link]();
}
}
}
Output
Enter the number of rows to print the pattern
5
** Printing the pattern... **
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Pattern 17:
package [Link];

import [Link];

public class Pattern17


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j < i; j++)
{
[Link](" ");
}

for (int k = i; k <= rows; k++)


{
[Link](k);
}
[Link]();
}
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
[Link](" ");
}

for (int k = i; k <= rows; k++)


{
[Link](k);
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
12345
2345
345
45
5
5
45
345
2345
12345
Pattern 18:
package [Link];

import [Link];

public class Pattern18


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j < i; j++)
{
[Link](" ");
}

for (int k = i; k <= rows; k++)


{
[Link](k + " ");
}
[Link]();
}
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
[Link](" ");
}

for (int k = i; k <= rows; k++)


{
[Link](k + " ");
}
[Link]();
}

}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
Pattern 19:
package [Link];

import [Link];

public class Pattern19


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = rows; i >= 1; i--)


{
for (int j = 1; j < i; j++)
{
[Link](" ");
}
for (int k = i; k <= rows; k++)
{
[Link](k + " ");
}
[Link]();
}

}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
Pattern 20:
package [Link];

import [Link];

public class Pattern20


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = rows; j > i; j--)
{
[Link](" ");
}
for (int k = 1; k <= i; k++)
{
[Link](k);
}
for (int l = i - 1; l >= 1; l--)
{
[Link](l);
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
121
12321
1234321
123454321
Pattern 21:
package [Link];

import [Link];

public class Pattern21


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
[Link](j % 2 + " ");
}

[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
Pattern 22:
package [Link];

import [Link];

public class Pattern22


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j < i; j++)
{
[Link]("0 ");
}
[Link](i + " ");
for (int k = i; k < rows; k++)
{
[Link]("0 ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
0 0 0 0 5
Pattern 23:
package [Link];

import [Link];

public class Pattern23


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);
// Get the number of rows from the user
[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = rows; j > i; j--)
{
[Link](1 + " ");
}

for (int k = 1; k <= i; k++)


{
[Link](i + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1 1 1 1 1
1 1 1 2 2
1 1 3 3 3
1 4 4 4 4
5 5 5 5 5
Pattern 24:
package [Link];

import [Link];

public class Pattern24


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = i; j <= rows; j++)
{
[Link](j + " ");
}
for (int k = rows - 1; k >= i; k--)
{
[Link](k + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1 2 3 4 5 4 3 2 1
2 3 4 5 4 3 2
3 4 5 4 3
4 5 4
5
Pattern 25:
package [Link];

import [Link];

public class Pattern25


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
for (int j = rows; j > i; j--)
{
[Link](" ");
}

for (int k = 1; k <= i; k++)


{
[Link](i + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Pattern 26:
package [Link];

import [Link];

public class Pattern26


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();

[Link]("** Printing the pattern... **");

for (int i = rows; i >= 1; i--)


{
for (int j = i; j < rows; j++)
{
[Link](j + " ");
}

for (int k = rows - i; k < rows; k++)


{
[Link](5 + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
5 5 5 5 5
4 5 5 5 5
3 4 5 5 5
2 3 4 5 5
1 2 3 4 5
Pattern 27:
package [Link];

import [Link];

public class Pattern27


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();


int k = 1;

[Link]("** Printing the pattern... **");

for (int i = 1; i <= rows; i++)


{
k=i;
for (int j = 1; j <= i; j++)
{
[Link](k + " ");
k = k + rows - j;
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Pattern 28:
package [Link];

import [Link];

public class Pattern28


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();


[Link]("** Printing the pattern... **");
int temp = 1;
for(int i=1; i<=rows/2+1; i++)
{
for(int j=1;j<=i;j++)
{
[Link](temp*j+" ");
}
[Link]();
temp++;
}
for(int i=1; i<=rows/2; i++)
{
for(int j=1;j<=rows/2+1-i;j++)
{
[Link](temp*j+" ");
}
[Link]();
temp++;
}
}
}
Output

Enter the number of rows to print the pattern


7
** Printing the pattern... **
1
2 4
3 6 9
4 8 12 16
5 10 15
6 12
7
Pattern 29:
package [Link];

import [Link];

public class Pattern29


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);
// Get the number of rows from the user
[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();


[Link]("** Printing the pattern... **");

for (int i = 0; i < rows; i++)


{
for (int j = 0; j <= i; j++)
{
if (j % 2 == 0)
{
[Link](1 + j * rows - (j - 1) * j / 2 + i - j +
" ");
} else
{
[Link](1 + j * rows - (j - 1) * j / 2 + rows -
1 - i + " ");
}
}

[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
Pattern 30:
package [Link];

import [Link];

public class Pattern30


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();


[Link]("** Printing the pattern... **");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if (j % 2 == 0)
[Link]((rows * (j)) + i + 1 + " ");
else
[Link]((rows * (j + 1)) - i + " ");
}
[Link]("\n");
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
Pattern 31:
package [Link];

import [Link];

public class Pattern31


{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner([Link]);

// Get the number of rows from the user


[Link]("Enter the number of rows to print the pattern ");

int rows = [Link]();


int temp = 0;
[Link]("** Printing the pattern... **");
for (int i = rows; i >= 1; i--)
{
for (int j = rows ; j >= i; j--)
{
[Link](j + " ");
temp =j;
}
for (int k = rows - i+1; k < rows; k++)
{
[Link](temp + " ");
}
[Link]();
}
}
}
Output

Enter the number of rows to print the pattern


5
** Printing the pattern... **
5 5 5 5 5
5 4 4 4 4
5 4 3 3 3
5 4 3 2 2
5 4 3 2 1

You might also like