0% found this document useful (0 votes)
9 views6 pages

Number Pattern

The document contains a Java program that prints patterns representing the digits 0 to 9 using asterisks. Each digit is formed by specific conditions in nested loops to determine where to place the asterisks. The program includes detailed comments explaining the logic behind the pattern for each digit.

Uploaded by

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

Number Pattern

The document contains a Java program that prints patterns representing the digits 0 to 9 using asterisks. Each digit is formed by specific conditions in nested loops to determine where to place the asterisks. The program includes detailed comments explaining the logic behind the pattern for each digit.

Uploaded by

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

public class NumberPattern {

public static void main(String[] args) {


int n = 5;
int m = n / 2;
// One
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i == 0 && j <= m) || i == n - 1 || j == m)
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
[Link]("\n");

// Two
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || i == m || (j == 0 && i >= m) || (j == n - 1 && i <= m))
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
[Link]("\n");

// Three
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || i == m || j == n - 1)
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
[Link]("\n");

// Four
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == m || j == m || (j == 0 && i <= m))
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
[Link]("\n");

// Five
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || i == m || (j == 0 && i <= m) || (j == n - 1 && i >= m))
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
[Link]("\n");

// Six
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == n - 1 || i == m || j == 0 || (j == n - 1 && i >= m))
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
[Link]("\n");

// Seven
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == (n - i))
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
[Link]("\n");

// Eight
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || i == m || j == 0 || j == n - 1)
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
[Link]("\n");

// Nine
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || i == m || (j == 0 && i <= m) || j == n - 1)
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
[Link]("\n");

// Zero
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == n - 1)
[Link]("* ");
else
[Link](" ");
}
[Link]();
}
[Link]("\n");
}
}

// Digit 1 logic

// Prints star in these conditions:

// 1. First row and up to center column: (i == 0 && j <= m)

// 2. All rows at center column: (j == m)

// 3. Last row: (i == n-1)

// Digit 2 logic

// Prints star in these conditions:

// 1. First row: (i == 0)
// 2. Center row: (i == m)

// 3. Last row: (i == n-1)

// 4. Leftmost column in bottom half: (j == 0 && i >= m)

// 5. Rightmost column in top half: (j == n-1 && i <= m)

// Digit 3 logic

// Prints star in these conditions:

// 1. First row: (i == 0)

// 2. Center row: (i == m)

// 3. Last row: (i == n-1)

// 4. Rightmost column: (j == n-1)

// Digit 4 logic

// Prints star in these conditions:

// 1. Center row: (i == m)

// 2. Center column: (j == m)

// 3. Leftmost column in upper half: (j == 0 && i <= m)

// Digit 5 logic

// Prints star in these conditions:

// 1. First row: (i == 0)

// 2. Center row: (i == m)

// 3. Last row: (i == n-1)

// 4. Leftmost column in top half: (j == 0 && i <= m)

// 5. Rightmost column in bottom half: (j == n-1 && i >= m)

// Digit 6 logic

// Prints star in these conditions:

// 1. Middle row: (i == m)

// 2. Last row: (i == n-1)

// 3. Leftmost column: (j == 0)
// 4. Rightmost column in bottom half: (j == n-1 && i >= m)

// Digit 7 logic

// Prints star in these conditions:

// 1. Top row: (i == 0)

// 2. Diagonal downward: (j == n - i)

// Digit 8 logic

// Prints star in these conditions:

// 1. Top row: (i == 0)

// 2. Middle row: (i == m)

// 3. Bottom row: (i == n-1)

// 4. Leftmost column: (j == 0)

// 5. Rightmost column: (j == n-1)

// Digit 9 logic

// Prints star in these conditions:

// 1. Top row: (i == 0)

// 2. Center row: (i == m)

// 3. Bottom row: (i == n-1)

// 4. Left column in top half: (j == 0 && i <= m)

// 5. Rightmost column: (j == n-1)

// Digit 0 logic

// Prints star in these conditions:

// 1. Top row: (i == 0)

// 2. Bottom row: (i == n-1)


// 3. Leftmost column: (j == 0)

// 4. Rightmost column: (j == n-1)

You might also like