Duplicate Number V Pattern in C

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

What You’ll Learn

How to print a V pattern where each row (except the first) prints the same number twice, with spaces expanding between the two sides.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
    1
   2 2
  3   3
 4     4
5       5
1

Complete C Program

Two loops print the left and right sides. The right side starts at 2 so the first row prints only one 1.

c
#include <stdio.h>

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

    for (i = 1; i <= 5; i++) {
        for (j = 5; j >= 1; j--) {
            if (i == j)
                printf("%d", j);
            else
                printf(" ");
        }
        for (k = 2; k <= 5; k++) {
            if (i == k)
                printf("%d", k);
            else
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Rows control the number printed

The outer loop sets i (1 to 5).

Rows
2

Left side prints once

We print when i == j while looping j = rows..1.

Left
3

Right side prints once

We print when i == k while looping k = 2..rows.

Right
4

Spaces fill the gap

Every other position prints a space, so the gap expands row by row, forming a V.

Spaces
=

Duplicate-number V

Rows 2..5 show the same number twice.

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 = rows; j >= 1; j--) {
            if (i == j)
                printf("%d", j);
            else
                printf(" ");
        }
        for (k = 2; k <= rows; k++) {
            if (i == k)
                printf("%d", k);
            else
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Replace numbers with * to print a classic V
  • Start the right loop at 1 to duplicate the top value too
  • Use two spaces per gap to make the V wider

Avoid

  • Starting the right loop at 1 if you want only one number on row 1
  • Using rows <= 0 without validation

Key Takeaways

1

Two diagonals are printed using conditional checks.

2

Rows 2..n print the number twice (duplicate effect).

3

Starting the right loop at 2 keeps the first row single.

4

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

❓ Frequently Asked Questions

Because the right side starts at k=2, so row 1 has no right-side match.
Start the right loop from k=1 instead of 2.
O(n²) for n rows.

Explore More C Number Patterns!

Practice diagonal-based patterns and spacing tricks.

All Number Patterns →
Did you know?

This is another “coordinate printing” pattern: you print only when a cell 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