V-Shaped Number Pattern in C

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Conditions + Loops

What You’ll Learn

How to print a V-shaped number pattern by placing numbers on two diagonals and printing spaces everywhere else.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
 1        1
  2     2
   3   3
    4 4
     5
1

Complete C Program

We use two loops per row: one for the left diagonal (i == j) and one for the right diagonal (i == k).

c
#include <stdio.h>

int main() {
    int i, j, k;

    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= 5; j++) {
            if (i == j)
                printf("%d", j);
            else
                printf(" ");
        }

        for (k = 4; k >= 1; k--) {
            if (i == k)
                printf("%d", k);
            else
                printf(" ");
        }

        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Loop through rows

The outer loop prints 5 lines.

Rows
2

Left diagonal (i == j)

We print the row number on the left diagonal by checking i == j.

Left
3

Right diagonal (i == k)

We print the matching value on the right diagonal by checking i == k.

Right
4

Avoid double-printing the bottom

The right-diagonal loop starts at 4 so the last row prints only one 5.

Detail
=

V shape

Spaces fill the rest of the grid so the diagonals stand out.

2

Variation — User Input Version

Read the number of rows using scanf().

c
#include <stdio.h>

int main() {
    int i, j, k;
    int rows;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= rows; j++) {
            if (i == j)
                printf("%d", j);
            else
                printf(" ");
        }

        for (k = rows - 1; k >= 1; k--) {
            if (i == k)
                printf("%d", k);
            else
                printf(" ");
        }

        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print asterisks instead of numbers for a classic V pattern
  • Use printf("%d ", ...) to add spacing
  • Make a thicker V by printing around the diagonals too

Avoid

  • Starting the right loop at rows (duplicates the center)
  • Using rows <= 0 without input validation

Key Takeaways

1

Two diagonal conditions create the V shape.

2

Spaces are printed everywhere else to highlight the diagonals.

3

Start the right diagonal at rows-1 to avoid duplicating the last number.

4

Nested loops over a grid lead to O(n²) time.

❓ Frequently Asked Questions

Both diagonals meet at the bottom. We avoid printing the same number twice by skipping it in the right-diagonal loop.
Yes. Change the condition to print on more positions (for example within a range around each diagonal).
O(n²) for n rows.

Explore More C Number Patterns!

Try more diagonal and symmetry patterns to master loop logic.

All Number Patterns →
Did you know?

This pattern is a simple example of drawing shapes by printing only when a coordinate matches a condition like i == j.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful