0% found this document useful (0 votes)
1 views2 pages

Java Number Patterns

The document contains Java programs for generating three different number patterns: Pattern A, Pattern B, and Pattern C. Each pattern is illustrated with a visual representation and accompanied by corresponding Java code to produce the output. The patterns vary in structure, showcasing different arrangements of numbers in ascending and descending order.

Uploaded by

gudduguptatata
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)
1 views2 pages

Java Number Patterns

The document contains Java programs for generating three different number patterns: Pattern A, Pattern B, and Pattern C. Each pattern is illustrated with a visual representation and accompanied by corresponding Java code to produce the output. The patterns vary in structure, showcasing different arrangements of numbers in ascending and descending order.

Uploaded by

gudduguptatata
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
You are on page 1/ 2

Java Programs for Number Patterns

Pattern A:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

Java Code:

public class PatternA {


public static void main(String[] args) {
int i, j;

// Upper part
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}

// Lower part
for (i = 4; i >= 1; i--) {
for (j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
Pattern B:
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

Java Code:

public class PatternB {


public static void main(String[] args) {
int i, j;
// Upper part
for (i = 5; i >= 1; i--) {
for (j = i; j >= 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}

// Lower part
for (i = 2; i <= 5; i++) {
for (j = i; j >= 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Pattern C:
1 2 3 4 5 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1

Java Code:

public class PatternC {


public static void main(String[] args) {
int i, j;
int n = 5;

for (i = 0; i < n; i++) {


// Left part (1 to n-i)
for (j = 1; j <= n - i; j++) {
System.out.print(j + " ");
}

// Spaces in between
for (j = 1; j <= 2 * i; j++) {
System.out.print(" ");
}

// Right part (n-i down to 1)


for (j = n - i; j >= 1; j--) {
System.out.print(j + " ");
}

System.out.println();
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy