0% found this document useful (0 votes)
25 views46 pages

Java Number Pattern Programs - W3schools

The document provides a series of Java programs that generate various number patterns based on user input for the number of rows. Each pattern example demonstrates different arrangements of numbers, including increasing sequences, decreasing sequences, and combinations of both. The document includes code snippets and expected outputs for each pattern example.

Uploaded by

Lalit Mahajan
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)
25 views46 pages

Java Number Pattern Programs - W3schools

The document provides a series of Java programs that generate various number patterns based on user input for the number of rows. Each pattern example demonstrates different arrangements of numbers, including increasing sequences, decreasing sequences, and combinations of both. The document includes code snippets and expected outputs for each pattern example.

Uploaded by

Lalit Mahajan
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

W3schools

Subscribe Youtube For Video Tutorials In Hindi

Menu

Java Number Pattern Programs


Pattern Example-1
Pattern

----Pattern is----
1
12
123
1234
12345
package com.w3spoint;

import [Link];

public class Test{


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Number of rows you want in this pat
int rows = [Link]();
[Link]("----Pattern is----");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
[Link](j+" ");
}

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Pattern Example-2
Pattern

----Pattern is----
1
22
333
4444
55555
package com.w3spoint;

import [Link];

public class Test


{
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Number of rows you want in this pat
int rows = [Link]();
[Link]("----Pattern is----");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
[Link](i+" ");
}

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Pattern Example-3
Pattern

----Pattern is----
1
12
123
1234
12345
1234
123
12
1
package com.w3spoint;

import [Link];

public class Test{


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Number of rows you want in this pat
int rows = [Link]();

[Link]("----Pattern is----");

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


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

[Link]();
}

// lower half of the pattern

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


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

[Link]();
}

[Link]();
}
}

Output
Number of rows you want in this pattern?
5
----Pattern is----
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 Example-4
Pattern
----Pattern is----
12345
1234
123
12
1
package com.w3spoint;

import [Link];

public class Test{


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Number of rows you want in this pat
int rows = [Link]();
[Link]("----Pattern is----");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
[Link](j+" ");
}

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Pattern Example-5
Pattern

----Pattern is----
54321
5432
543
54
5
package com.w3spoint;

import [Link];

public class Test{


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

[Link]("Number of rows you want in this pat

int rows = [Link]();

[Link]("----Pattern is----");

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


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

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5

Pattern Example-6
Pattern

----Pattern is----
5
54
543
5432
54321
package com.w3spoint;

import [Link];

public class Test{


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

[Link]("Number of rows you want in this pat

int rows = [Link]();

[Link]("----Pattern is----");

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


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

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

Pattern Example-7
Pattern

----Pattern is----
54321
4321
321
21
1
package com.w3spoint;

import [Link];

public class Test{


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

[Link]("Number of rows you want in this pat

int rows = [Link]();

[Link]("----Pattern is----");

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


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

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

Pattern Example-8
Pattern

----Pattern is----
12345
1234
123
12
1
12
123
1234
12345
package com.w3spoint;

import [Link];

public class Test{


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Number of rows you want in this pat
int rows = [Link]();
[Link]("----Pattern is----");

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


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

[Link]();
}

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


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

[Link]();
}

[Link]();
}
}

Output
Number of rows you want in this pattern?
5
----Pattern is----
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Pattern Example-9
Pattern
----Pattern is----
1
121
12321
1234321
123454321
package com.w3spoint;

import [Link];

public class Test


{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Number of rows you want in this pat

int rows = [Link]();

[Link]("----Pattern is----");

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


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

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


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

[Link]();
}
[Link]();
}
}

Output
Number of rows you want in this pattern?
5
----Pattern is----
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 Example-10
Pattern
----Pattern is----
1
21
321
4321
54321
package com.w3spoint;

import [Link];

public class Test{


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

[Link]("Number of rows you want in this pat

int rows = [Link]();

[Link]("----Pattern is----");

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


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

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

Pattern Example-11
Pattern

----Pattern is----
12345
2345
345
45
5
45
345
2345
12345
package com.w3spoint;

import [Link];

public class Test{


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

[Link]("Number of rows you want in this pat

int rows = [Link]();

[Link]("----Pattern is----");
for (int i = 1; i <= rows; i++)
{

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


{
[Link](" ");
}

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


{
[Link](j);
}
[Link]();
}

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


{

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


{
[Link](" ");
}

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


{
[Link](j);
}

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
12345
2345
345
45
5
45
345
2345
12345

Pattern Example-12
Pattern
----Pattern is----
12345
2345
345
45
5
45
345
2345
12345
package com.w3spoint;

import [Link];

public class Test{


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

[Link]("Number of rows you want in this pat

int rows = [Link]();

[Link]("----Pattern is----");

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


{

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


{
[Link](" ");
}

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


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

[Link]();
}

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


{

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


{
[Link](" ");
}

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


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

[Link]();
}

[Link]();
Output
Number of rows you want in this pattern?
5
----Pattern is----
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5

Pattern Example-13
Pattern
----Pattern is----
1
10
101
1010
10101
package com.w3spoint;

import [Link];

public class Test{


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

[Link]("Number of rows you want in this pat

int rows = [Link]();


[Link]("----Pattern is----");

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


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

[Link]();
}
[Link]();
}
}

Output
Number of rows you want in this pattern?
5
----Pattern is----
1
10
101
1010
10101

Pattern Example-14
Pattern

----Pattern is----
10101
01010
10101
01010
10101
package com.w3spoint;

import [Link];

public class Test{


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

[Link]("Number of rows you want in this pat

int rows = [Link]();


[Link]("----Pattern is----");
for (int i = 1; i <= rows; i++)
{
int num;

if(i%2 == 0)
{
num = 0;

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


{
[Link](num);

num = (num == 0)? 1 : 0;


}
}
else
{
num = 1;

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


{
[Link](num);

num = (num == 0)? 1 : 0;


}
}

[Link]();
}

[Link]();
}
}

Output
Number of rows you want in this pattern?
5
----Pattern is----
10101
01010
10101
01010
10101
Pattern Example-15
Pattern

----Pattern is----
11111
11122
11333
14444
55555
package com.w3spoint;
import [Link];

public class Test{


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

[Link]("Number of rows you want in this pat

int rows = [Link]();


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

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


{
[Link](i);
}

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
11111
11122
11333
14444
55555

Pattern Example-16
Pattern

----Pattern is----
00000
01000
00200
00030
00004
package com.w3spoint;
import [Link];

public class Test{


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

[Link]("Number of rows you want in this pat

int rows = [Link]();


[Link]("----Pattern is----");

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


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

[Link]();
}
[Link]();
}
}

Output
Number of rows you want in this pattern?
5
----Pattern is----
00000
01000
00200
00030
00004

Pattern Example-17
Pattern

----Pattern is----
1
26
3 7 10
4 8 11 13
5 9 12 14 15
package com.w3spoint;
import [Link];

public class Test{


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Number of rows you want in this pat

int rows = [Link]();


[Link]("----Pattern is----");
for (int i = 1; i <= rows; i++)
{
int num = i;

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


{
[Link](num+" ");

num = num+rows-j;
}

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

Pattern Example-18
Pattern

----Pattern is----
1
26
3 7 10
4 8 11 13
5 9 12 14 15
package com.w3spoint;
import [Link];

public class Test{


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Number of rows you want in this pat

int rows = [Link]();


[Link]("----Pattern is----");
for (int i = 1; i <= rows; i++)
{
int num = i;

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


{
[Link](num+" ");

num = num+rows-j;
}

[Link]();
}

[Link]();
}
}
Output
Number of rows you want in this pattern?
5
----Pattern is----
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

Please follow and like us:

Content Protection by [Link]

Related Posts
Data Statistics And Analysis With Java And Python

Deploying Java Applications To AWS Elastic Beanstalk

Echo Java_home In Windows Cmd

No Java Files Found That Extend CordovaActivity

Max Value In Priority Queue Java

Save Map In File Java


Spigot Spawn Entity

Hardware Assisted Virtualization And Data Execution Protection Must Be Enabled In The BIOS

Regrex For Letter In Java

SpringBoot Starter Jpa

Divide With Float Java

Spring Boot Maven Run With Profile

Bukkit Inventory Set Name

Best ASCII Art Characters

Detect Operating System In Java

Show Dialog Fragment From Adapter

Java Stream Find Specific Element

Upgrade Java 8 To 11 In Ubuntu

Copy To Clipboard Java

Exit From Jshell

© 2024 W3schools. All Rights Reserved. SiteMap

You might also like