Hard Number Patterns in C, Java, Python [Interview Questions]

Learn how to solve the hardest number patterns easily that are mostly asked by interviewers in IT Industries.30 min


number patterns in c with examples

In interviews, once you are giving proper answers to beginner or intermediate questions, the interviewer might start asking the hardest questions. 

In this article, I have covered the hardest number patterns that are asked by interviewers to satisfy their ego or attitude. That is the fact, but nobody will tell you about it. Don’t worry — just go with better preparation and improve your technical skills.

1. Pattern: Custom Descending Number Pattern

Expected Output: 

1234567
12345
123
1

Solution in C Program:

#include <stdio.h>

int main() {
    // Outer loop for the number of rows (starting from 7 to 1)
    for (int i = 7; i >= 1; i -= 2) {
        // Inner loop to print numbers from 1 to i
        for (int j = 1; j <= i; j++) {
            printf("%d", j); // Print the current number
        }
        printf("n"); // Move to the next line after each row
    }
    return 0;
}

Solution in Java:

public class NumberPattern {
    public static void main(String[] args) {
        // Outer loop for the number of rows (starting from 7 down to 1)
        for (int i = 7; i >= 1; i -= 2) {
            // Inner loop to print numbers from 1 to i
            for (int j = 1; j <= i; j++) {
                System.out.print(j); // Print the current number
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Solution in Python:

# Outer loop for the number of rows (starting from 7 down to 1)
for i in range(7, 0, -2):
    # Inner loop to print numbers from 1 to i
    for j in range(1, i + 1):
        print(j, end='')  # Print the current number, stay on the same line
    print()  # Move to the next line after each row

2. Pattern: Mixed Ascending and Descending Number Pattern

Expected Output:

12345
4321
123
21
1

Solution in C Program:

#include <stdio.h>

int main() {
    // Outer loop for the number of rows (starting from 5 to 1)
    for (int i = 5; i >= 1; i -= 2) {
        // Print ascending numbers for odd rows
        if ((5 - i) % 4 == 0) {
            for (int j = 1; j <= i; j++) {
                printf("%d", j); // Print ascending number
            }
        }
        // Print descending numbers for even rows
        else {
            for (int j = i; j >= 1; j--) {
                printf("%d", j); // Print descending number
            }
        }
        printf("n"); // Move to the next line after each row
    }
    return 0;
}

Solution in Java:

public class NumberPattern {
    public static void main(String[] args) {
        // Outer loop for the number of rows (starting from 5 to 1)
        for (int i = 5; i >= 1; i -= 2) {
            // Print ascending numbers for odd rows
            if ((5 - i) % 4 == 0) {
                for (int j = 1; j <= i; j++) {
                    System.out.print(j); // Print ascending number
                }
            }
            // Print descending numbers for even rows
            else {
                for (int j = i; j >= 1; j--) {
                    System.out.print(j); // Print descending number
                }
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Solution in Python:

# Outer loop for the number of rows (starting from 5 down to 1)
for i in range(5, 0, -2):
    # Print ascending numbers for odd rows
    if (5 - i) % 4 == 0:
        for j in range(1, i + 1):
            print(j, end='')  # Print ascending number, stay on the same line
    # Print descending numbers for even rows
    else:
        for j in range(i, 0, -1):
            print(j, end='')  # Print descending number, stay on the same line
    print()  # Move to the next line after each row

3. Pattern: Alternating Binary Pattern

Expected Output:

1
01
101
0101

Solution in C Program:

#include <stdio.h>

int main() {
    // Outer loop for the number of rows (1 to 4)
    for (int i = 1; i <= 4; i++) {
        // Inner loop to print alternating 1 and 0
        for (int j = 1; j <= i; j++) {
            if (j % 2 == 1) {
                printf("1"); // Print 1 for odd positions
            } else {
                printf("0"); // Print 0 for even positions
            }
        }
        printf("n"); // Move to the next line after each row
    }
    return 0;
}

Solution in Java:

public class BinaryPattern {
    public static void main(String[] args) {
        // Outer loop for the number of rows (1 to 4)
        for (int i = 1; i <= 4; i++) {
            // Inner loop to print alternating 1 and 0
            for (int j = 1; j <= i; j++) {
                if (j % 2 == 1) {
                    System.out.print("1"); // Print 1 for odd positions
                } else {
                    System.out.print("0"); // Print 0 for even positions
                }
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Solution in Python:

# Outer loop for the number of rows (1 to 4)
for i in range(1, 5):
    # Inner loop to print alternating 1 and 0
    for j in range(1, i + 1):
        if j % 2 == 1:
            print(1, end='')  # Print 1 for odd positions
        else:
            print(0, end='')  # Print 0 for even positions
    print()  # Move to the next line after each row

4. Pattern: Descending Odd Number Pattern

Expected Output:

13579
3579
579
79
9

Solution in C Program:

#include <stdio.h>

int main() {
    // Outer loop for the number of rows (starting from 5 down to 1)
    for (int i = 1; i <= 5; i++) {
        // Inner loop to print odd numbers starting from 2*i-1 to 9
        for (int j = 2 * i - 1; j <= 9; j += 2) {
            printf("%d", j); // Print odd numbers starting from 2*i - 1
        }
        printf("n"); // Move to the next line after each row
    }
    return 0;
}

Solution in Java:

public class OddNumberPattern {
    public static void main(String[] args) {
        // Outer loop for the number of rows (starting from 5 down to 1)
        for (int i = 1; i <= 5; i++) {
            // Inner loop to print odd numbers starting from 2*i-1 to 9
            for (int j = 2 * i - 1; j <= 9; j += 2) {
                System.out.print(j); // Print odd numbers starting from 2*i - 1
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Solution in Python:

# Outer loop for the number of rows (starting from 5 down to 1)
for i in range(1, 6):
    # Inner loop to print odd numbers starting from 2*i-1 to 9
    for j in range(2 * i - 1, 10, 2):
        print(j, end='')  # Print odd numbers starting from 2*i - 1
    print()  # Move to the next line after each row

5. Pattern: Alternating Odd and Even Number Pattern

Expected Output:

1
2 4
1 3 5
2 4 6 8
1 3 5 7 9

Solution in C Program:

#include <stdio.h>

int main() {
    // Outer loop for the number of rows (1 to 5)
    for (int i = 1; i <= 5; i++) {
        // Inner loop to print numbers based on row parity (odd or even)
        for (int j = 1; j <= i; j++) {
            if (i % 2 == 1) { // Odd rows (1st, 3rd, 5th)
                printf("%d ", 2 * j - 1); // Print odd numbers (1, 3, 5, etc.)
            } else { // Even rows (2nd, 4th)
                printf("%d ", 2 * j); // Print even numbers (2, 4, 6, etc.)
            }
        }
        printf("n"); // Move to the next line after each row
    }
    return 0;
}

Solution in Java:

public class NumberPattern {
    public static void main(String[] args) {
        // Outer loop for the number of rows (1 to 5)
        for (int i = 1; i <= 5; i++) {
            // Inner loop to print numbers based on row parity (odd or even)
            for (int j = 1; j <= i; j++) {
                if (i % 2 == 1) { // Odd rows (1st, 3rd, 5th)
                    System.out.print((2 * j - 1) + " "); // Print odd numbers (1, 3, 5, etc.)
                } else { // Even rows (2nd, 4th)
                    System.out.print((2 * j) + " "); // Print even numbers (2, 4, 6, etc.)
                }
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Solution in Python:

# Outer loop for the number of rows (1 to 5)
for i in range(1, 6):
    # Inner loop to print numbers based on row parity (odd or even)
    for j in range(1, i + 1):
        if i % 2 == 1:  # Odd rows (1st, 3rd, 5th)
            print(2 * j - 1, end=' ')  # Print odd numbers (1, 3, 5, etc.)
        else:  # Even rows (2nd, 4th)
            print(2 * j, end=' ')  # Print even numbers (2, 4, 6, etc.)
    print()  # Move to the next line after each row

6. Pattern: Descending Number Pattern with 5s

Expected Output:

55555
45555
34555
23455
12345

Solution in C Program:

#include <stdio.h>

int main() {
    // Outer loop for the number of rows (1 to 5)
    for (int i = 5; i >= 1; i--) {
        // Print descending numbers starting from i
        for (int j = i; j >= 1; j--) {
            printf("%d", j); // Print the descending number
        }
        // Print 5s for the remaining positions
        for (int j = i + 1; j <= 5; j++) {
            printf("5"); // Print 5 for the remaining positions
        }
        printf("n"); // Move to the next line after each row
    }
    return 0;
}

Solution in Java:

public class NumberPattern {
    public static void main(String[] args) {
        // Outer loop for the number of rows (5 down to 1)
        for (int i = 5; i >= 1; i--) {
            // Print descending numbers starting from i
            for (int j = i; j >= 1; j--) {
                System.out.print(j); // Print the descending number
            }
            // Print 5s for the remaining positions
            for (int j = i + 1; j <= 5; j++) {
                System.out.print("5"); // Print 5 for the remaining positions
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Solution in Python:

# Outer loop for the number of rows (5 down to 1)
for i in range(5, 0, -1):
    # Print descending numbers starting from i
    for j in range(i, 0, -1):
        print(j, end='')  # Print the descending number
    # Print 5s for the remaining positions
    for j in range(i + 1, 6):
        print(5, end='')  # Print 5 for the remaining positions
    print()  # Move to the next line after each row

7. Pattern: Binary Alternating Pattern

Expected Output:

1
10
101
1010
10101

Solution in C Program:

#include <stdio.h>

int main() {
    // Outer loop for the number of rows (1 to 5)
    for (int i = 1; i <= 5; i++) {
        // Inner loop to print alternating 1 and 0
        for (int j = 1; j <= i; j++) {
            if (j % 2 == 1) {
                printf("1"); // Print 1 for odd positions
            } else {
                printf("0"); // Print 0 for even positions
            }
        }
        printf("n"); // Move to the next line after each row
    }
    return 0;
}

Solution in Java:

public class BinaryAlternatingPattern {
    public static void main(String[] args) {
        // Outer loop for the number of rows (1 to 5)
        for (int i = 1; i <= 5; i++) {
            // Inner loop to print alternating 1 and 0
            for (int j = 1; j <= i; j++) {
                if (j % 2 == 1) {
                    System.out.print("1"); // Print 1 for odd positions
                } else {
                    System.out.print("0"); // Print 0 for even positions
                }
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Solution in Python:

# Outer loop for the number of rows (1 to 5)
for i in range(1, 6):
    # Inner loop to print alternating 1 and 0
    for j in range(1, i + 1):
        if j % 2 == 1:
            print(1, end='')  # Print 1 for odd positions
        else:
            print(0, end='')  # Print 0 for even positions
    print()  # Move to the next line after each row

8. Pattern: Sequential Number Pattern

Expected Output:

1
23
456
78910

Solution in C Program:

#include <stdio.h>

int main() {
    int num = 1; // Start with 1
    
    // Outer loop for the number of rows (1 to 4)
    for (int i = 1; i <= 4; i++) {
        // Inner loop to print numbers in each row
        for (int j = 1; j <= i; j++) {
            printf("%d", num); // Print current number
            num++; // Increment the number for the next position
        }
        printf("n"); // Move to the next line after each row
    }
    return 0;
}

Solution in Java:

public class SequentialNumberPattern {
    public static void main(String[] args) {
        int num = 1; // Start with 1
        
        // Outer loop for the number of rows (1 to 4)
        for (int i = 1; i <= 4; i++) {
            // Inner loop to print numbers in each row
            for (int j = 1; j <= i; j++) {
                System.out.print(num); // Print current number
                num++; // Increment the number for the next position
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Solution in Python:

num = 1  # Start with 1

# Outer loop for the number of rows (1 to 4)
for i in range(1, 5):
    # Inner loop to print numbers in each row
    for j in range(1, i + 1):
        print(num, end='')  # Print current number
        num += 1  # Increment the number for the next position
    print()  # Move to the next line after each row

9. Pattern: Palindromic Number Pattern with Stars

Expected Output:

12344321
123**321
12****21
1******1

Solution in C Program:

#include <stdio.h>

int main() {
    // Outer loop for the number of rows (1 to 4)
    for (int i = 1; i <= 4; i++) {
        // Print increasing numbers from 1 to (5 - i)
        for (int j = 1; j <= 5 - i; j++) {
            printf("%d", j);
        }

        // Print stars in the middle
        for (int j = 1; j <= 2 * i - 2; j++) {
            printf("*");
        }

        // Print decreasing numbers from (5 - i) to 1
        for (int j = 5 - i; j >= 1; j--) {
            printf("%d", j);
        }

        printf("n"); // Move to the next line after each row
    }
    return 0;
}

Solution in Java:

public class PalindromicNumberPattern {
    public static void main(String[] args) {
        // Outer loop for the number of rows (1 to 4)
        for (int i = 1; i <= 4; i++) {
            // Print increasing numbers from 1 to (5 - i)
            for (int j = 1; j <= 5 - i; j++) {
                System.out.print(j);
            }

            // Print stars in the middle
            for (int j = 1; j <= 2 * i - 2; j++) {
                System.out.print("*");
            }

            // Print decreasing numbers from (5 - i) to 1
            for (int j = 5 - i; j >= 1; j--) {
                System.out.print(j);
            }

            System.out.println(); // Move to the next line after each row
        }
    }
}

Solution in Python:

# Outer loop for the number of rows (1 to 4)
for i in range(1, 5):
    # Print increasing numbers from 1 to (5 - i)
    for j in range(1, 5 - i + 1):
        print(j, end='')

    # Print stars in the middle
    for j in range(1, 2 * i):
        print("*", end='')

    # Print decreasing numbers from (5 - i) to 1
    for j in range(5 - i, 0, -1):
        print(j, end='')

    print()  # Move to the next line after each row

10. Pattern: Right-Aligned Number Pattern

Expected Output:

    1
  2 3 4
5 6 7 8 9

Solution in C Program:

#include <stdio.h>

int main() {
    int num = 1;  // Start with 1
    // Outer loop for the number of rows (3 rows)
    for (int i = 1; i <= 3; i++) {
        // Print leading spaces for right alignment
        for (int j = 1; j <= 3 - i; j++) {
            printf(" ");  // Print spaces to right-align the numbers
        }

        // Print the numbers in the row
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("%d ", num);  // Print the current number
            num++;  // Increment the number
        }

        printf("n");  // Move to the next line after each row
    }
    return 0;
}

Solution in Java:

public class RightAlignedNumberPattern {
    public static void main(String[] args) {
        int num = 1; // Start with 1

        // Outer loop for the number of rows (3 rows)
        for (int i = 1; i <= 3; i++) {
            // Print leading spaces for right alignment
            for (int j = 1; j <= 3 - i; j++) {
                System.out.print(" ");  // Print spaces to right-align the numbers
            }

            // Print the numbers in the row
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print(num + " ");  // Print the current number
                num++;  // Increment the number
            }

            System.out.println();  // Move to the next line after each row
        }
    }
}

Solution in Python:

num = 1  # Start with 1

# Outer loop for the number of rows (3 rows)
for i in range(1, 4):
    # Print leading spaces for right alignment
    for j in range(1, 4 - i):
        print(" ", end="")  # Print spaces to right-align the numbers

    # Print the numbers in the row
    for j in range(1, 2 * i):
        print(num, end=" ")  # Print the current number
        num += 1  # Increment the number

    print()  # Move to the next line after each row

11. Pattern: Column-wise Number Pattern

Expected Output:

1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

Solution in C Program:

#include <stdio.h>

int main() {
    int num = 1;  // Start with 1
    int matrix[5][5];  // To store the pattern numbers

    // Fill the matrix with the correct pattern
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j <= i; j++) {
            matrix[j][i] = num;  // Fill column-wise
            num++;  // Increment the number
        }
    }

    // Print the matrix to show the pattern
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j <= i; j++) {
            printf("%d ", matrix[j][i]);  // Print the numbers in the correct order
        }
        printf("n");  // Move to the next line after each row
    }

    return 0;
}

Solution in Java:

public class ColumnWiseNumberPattern {
    public static void main(String[] args) {
        int num = 1;  // Start with 1
        int[][] matrix = new int[5][5];  // To store the pattern numbers

        // Fill the matrix with the correct pattern
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j <= i; j++) {
                matrix[j][i] = num;  // Fill column-wise
                num++;  // Increment the number
            }
        }

        // Print the matrix to show the pattern
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(matrix[j][i] + " ");  // Print the numbers in the correct order
            }
            System.out.println();  // Move to the next line after each row
        }
    }
}

Solution in Python:

num = 1  # Start with 1
matrix = [[0 for _ in range(5)] for _ in range(5)]  # To store the pattern numbers

# Fill the matrix with the correct pattern
for i in range(5):
    for j in range(i + 1):
        matrix[j][i] = num  # Fill column-wise
        num += 1  # Increment the number

# Print the matrix to show the pattern
for i in range(5):
    for j in range(i + 1):
        print(matrix[j][i], end=" ")  # Print the numbers in the correct order
    print()  # Move to the next line after each row

Oops… I forgot to add a 12-number pattern in the image. 

12. Pattern: Diamond Number Pattern

Expected Output:

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

Solution in C Program:

#include <stdio.h>

int main() {
    int n = 5; // The maximum row length (central row length)
    
    // Upper half including the middle row
    for (int i = 1; i <= n; i++) {
        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }
        // Print increasing numbers
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("%d", j);
        }
        printf("n");
    }

    // Lower half (excluding the middle row)
    for (int i = n - 1; i >= 1; i--) {
        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }
        // Print increasing numbers
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("%d", j);
        }
        printf("n");
    }

    return 0;
}

Solution in Java:

public class DiamondNumberPattern {
    public static void main(String[] args) {
        int n = 5; // The maximum row length (central row length)
        
        // Upper half including the middle row
        for (int i = 1; i <= n; i++) {
            // Print leading spaces
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // Print increasing numbers
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print(j);
            }
            System.out.println();
        }

        // Lower half (excluding the middle row)
        for (int i = n - 1; i >= 1; i--) {
            // Print leading spaces
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            // Print increasing numbers
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Solution in Python:

n = 5  # The maximum row length (central row length)

# Upper half including the middle row
for i in range(1, n + 1):
    # Print leading spaces
    print(" " * (n - i), end="")
    # Print increasing numbers
    for j in range(1, 2 * i):
        print(j, end="")
    print()

# Lower half (excluding the middle row)
for i in range(n - 1, 0, -1):
    # Print leading spaces
    print(" " * (n - i), end="")
    # Print increasing numbers
    for j in range(1, 2 * i):
        print(j, end="")
    print()

Read below for more…


13. Pattern: Reverse Pyramid Pattern with Stars

Expected Output:

5432*
543*1
54*21
5*321
*4321

Solution in C Program:

#include <stdio.h>

int main() {
    // Outer loop for the number of rows (5 rows)
    for (int i = 0; i < 5; i++) {
        // Print decreasing numbers
        for (int j = 5; j > i; j--) {
            printf("%d", j);
        }

        // Print the star in the appropriate position
        printf("*");

        // Print the remaining decreasing numbers after the star
        for (int j = i + 1; j < 5; j++) {
            printf("%d", 5 - j);
        }

        printf("n");  // Move to the next line after each row
    }

    return 0;
}

Solution in Java:

public class ReversePyramidPatternWithStars {
    public static void main(String[] args) {
        // Outer loop for the number of rows (5 rows)
        for (int i = 0; i < 5; i++) {
            // Print decreasing numbers
            for (int j = 5; j > i; j--) {
                System.out.print(j);
            }

            // Print the star in the appropriate position
            System.out.print("*");

            // Print the remaining decreasing numbers after the star
            for (int j = i + 1; j < 5; j++) {
                System.out.print(5 - j);
            }

            System.out.println();  // Move to the next line after each row
        }
    }
}

Solution in Python:

# Outer loop for the number of rows (5 rows)
for i in range(5):
    # Print decreasing numbers
    for j in range(5, i, -1):
        print(j, end="")

    # Print the star in the appropriate position
    print("*", end="")

    # Print the remaining decreasing numbers after the star
    for j in range(i + 1, 5):
        print(5 - j, end="")

    print()  # Move to the next line after each row

14. Pattern: Symmetrical Diamond Number Pattern

Expected Output:

1        1
12      21
123    321
1234  4321
1234554321

Solution in C Program:

#include <stdio.h>

int main() {
    int n = 5;  // Number of rows
    
    // Upper half including the middle row
    for (int i = 1; i <= n; i++) {
        // Print the increasing numbers on the left side
        for (int j = 1; j <= i; j++) {
            printf("%d", j);
        }

        // Print spaces in the middle
        for (int j = 1; j <= 2 * (n - i); j++) {
            printf(" ");
        }

        // Print the decreasing numbers on the right side
        for (int j = i; j >= 1; j--) {
            printf("%d", j);
        }

        printf("n");  // Move to the next line after each row
    }

    return 0;
}

Solution in Java:

public class SymmetricalDiamondNumberPattern {
    public static void main(String[] args) {
        int n = 5;  // Number of rows

        // Upper half including the middle row
        for (int i = 1; i <= n; i++) {
            // Print the increasing numbers on the left side
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }

            // Print spaces in the middle
            for (int j = 1; j <= 2 * (n - i); j++) {
                System.out.print(" ");
            }

            // Print the decreasing numbers on the right side
            for (int j = i; j >= 1; j--) {
                System.out.print(j);
            }

            System.out.println();  // Move to the next line after each row
        }
    }
}

Solution in Python:

n = 5  # Number of rows

# Upper half including the middle row
for i in range(1, n + 1):
    # Print the increasing numbers on the left side
    for j in range(1, i + 1):
        print(j, end="")

    # Print spaces in the middle
    for j in range(2 * (n - i)):
        print(" ", end="")

    # Print the decreasing numbers on the right side
    for j in range(i, 0, -1):
        print(j, end="")

    print()  # Move to the next line after each row

15. Pattern: Symmetrical Number-Star Pattern

Expected Output:

1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1

Solution in C Program:

#include <stdio.h>

int main() {
    int n = 4; // The maximum number in the middle row
    
    // Upper half including the middle row
    for (int i = 1; i <= n; i++) {
        // Print the number 'i' repeatedly with '*' in between
        for (int j = 1; j <= i; j++) {
            printf("%d", i);
            if (j < i) {
                printf("*");
            }
        }
        printf("n");
    }

    // Lower half (excluding the middle row)
    for (int i = n; i >= 1; i--) {
        // Print the number 'i' repeatedly with '*' in between
        for (int j = 1; j <= i; j++) {
            printf("%d", i);
            if (j < i) {
                printf("*");
            }
        }
        printf("n");
    }

    return 0;
}

Solution in Java:

public class SymmetricalNumberStarPattern {
    public static void main(String[] args) {
        int n = 4; // The maximum number in the middle row

        // Upper half including the middle row
        for (int i = 1; i <= n; i++) {
            // Print the number 'i' repeatedly with '*' in between
            for (int j = 1; j <= i; j++) {
                System.out.print(i);
                if (j < i) {
                    System.out.print("*");
                }
            }
            System.out.println();
        }

        // Lower half (excluding the middle row)
        for (int i = n; i >= 1; i--) {
            // Print the number 'i' repeatedly with '*' in between
            for (int j = 1; j <= i; j++) {
                System.out.print(i);
                if (j < i) {
                    System.out.print("*");
                }
            }
            System.out.println();
        }
    }
}

Solution in Python:

n = 4  # The maximum number in the middle row

# Upper half including the middle row
for i in range(1, n + 1):
    # Print the number 'i' repeatedly with '*' in between
    for j in range(1, i + 1):
        print(i, end="")
        if j < i:
            print("*", end="")
    print()  # Move to the next line after each row

# Lower half (excluding the middle row)
for i in range(n, 0, -1):
    # Print the number 'i' repeatedly with '*' in between
    for j in range(1, i + 1):
        print(i, end="")
        if j < i:
            print("*", end="")
    print()  # Move to the next line after each row

16. Pattern: Right-Angled Triangle Number Pattern

Expected Output:

             1
           2 3
         4 5 6
      7 8 9 10
11 12 13 14 15

Solution in C Program:

#include <stdio.h>

int main() {
    int n = 5;  // Number of rows
    int num = 1;  // Start number

    // Loop through each row
    for (int i = 1; i <= n; i++) {
        // Print leading spaces
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }

        // Print the numbers in each row
        for (int j = 1; j <= i; j++) {
            printf("%d ", num);
            num++;  // Increment the number
        }

        printf("n");  // Move to the next line after each row
    }

    return 0;
}

Solution in Java:

public class RightAngledTriangleNumberPattern {
    public static void main(String[] args) {
        int n = 5;  // Number of rows
        int num = 1;  // Start number

        // Loop through each row
        for (int i = 1; i <= n; i++) {
            // Print leading spaces
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }

            // Print the numbers in each row
            for (int j = 1; j <= i; j++) {
                System.out.print(num + " ");
                num++;  // Increment the number
            }

            System.out.println();  // Move to the next line after each row
        }
    }
}

Solution in Python:

n = 5  # Number of rows
num = 1  # Start number

# Loop through each row
for i in range(1, n + 1):
    # Print leading spaces
    print(" " * (n - i), end="")

    # Print the numbers in each row
    for j in range(1, i + 1):
        print(num, end=" ")
        num += 1  # Increment the number

    print()  # Move to the next line after each row

17. Pattern: Rectangle Border with Hollow Center

Expected Output:

11111
1   1
1   1
1   1
11111

Solution in C Program:

#include <stdio.h>

int main() {
    int n = 5;  // Size of the square (5x5)

    // Outer loop for the rows
    for (int i = 1; i <= n; i++) {
        // Inner loop for the columns
        for (int j = 1; j <= n; j++) {
            // Print 1 at the border or corners, otherwise print space
            if (i == 1 || i == n || j == 1 || j == n) {
                printf("1");
            } else {
                printf(" ");
            }
        }
        printf("n");  // Move to the next line after each row
    }

    return 0;
}

Solution in Java:

public class SquareBorderWithHollowCenter {
    public static void main(String[] args) {
        int n = 5;  // Size of the square (5x5)

        // Outer loop for the rows
        for (int i = 1; i <= n; i++) {
            // Inner loop for the columns
            for (int j = 1; j <= n; j++) {
                // Print 1 at the border or corners, otherwise print space
                if (i == 1 || i == n || j == 1 || j == n) {
                    System.out.print("1");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();  // Move to the next line after each row
        }
    }
}

Solution in Python:

n = 5  # Size of the square (5x5)

# Outer loop for the rows
for i in range(1, n + 1):
    # Inner loop for the columns
    for j in range(1, n + 1):
        # Print 1 at the border or corners, otherwise print space
        if i == 1 or i == n or j == 1 or j == n:
            print("1", end="")
        else:
            print(" ", end="")
    print()  # Move to the next line after each row

Hope you find all the number patterns and their coding written in python, Java, or C programming most helpful for your interview preparation. Good luck with your career.

©️ 2025 — Hard number pattern in C, Java, and Python By Rakshit Shah.

You may also like,

1. Number Patterns in C, Java, and Python Programming
Learn how to create popular number patterns with step-by-step solutions in C, Java, and Python programming languages.

2. Alphabet Patterns — Java, Python, and C program
Learn how to code different alphabet patterns in Java, Python, and C programming.

3. Check Pair of Brackets (Well-Formatted) in an Expression
A simple program to check the pair of brackets using Stack in Java, Python, and C#.

adsense


Discover more from 9Mood

Subscribe to get the latest posts sent to your email.


Like it? Share with your friends!

What's Your Reaction?

Lol Lol
0
Lol
WTF WTF
0
WTF
Cute Cute
0
Cute
Love Love
0
Love
Vomit Vomit
0
Vomit
Cry Cry
0
Cry
Wow Wow
0
Wow
Fail Fail
0
Fail
Angry Angry
0
Angry
Rakshit Shah

Legend

Hey Moodies, Kem chho ? - Majama? (Yeah, You guessed Right! I am from Gujarat, India) 25, Computer Engineer, Foodie, Gamer, Coder and may be a Traveller . > If I can’t, who else will? < You can reach out me by “Rakshitshah94” on 9MOodQuoraMediumGithubInstagramsnapchattwitter, Even you can also google it to see me. I am everywhere, But I am not God. Feel free to text me.

0 Comments

Leave a Reply

Choose A Format
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Ranked List
Upvote or downvote to decide the best list item
Open List
Submit your own item and vote up for the best submission
Countdown
The Classic Internet Countdowns
Meme
Upload your own images to make custom memes
Poll
Voting to make decisions or determine opinions
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Personality quiz
Series of questions that intends to reveal something about the personality
is avocado good for breakfast? Sustainability Tips for Living Green Daily Photos Taken At Right Moment