Duplicate Number V Pattern in C

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:
1
2 2
3 3
4 4
5 5Complete C Program
Two loops print the left and right sides. The right side starts at 2 so the first row prints only one 1.
#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
Rows control the number printed
The outer loop sets i (1 to 5).
Left side prints once
We print when i == j while looping j = rows..1.
Right side prints once
We print when i == k while looping k = 2..rows.
Spaces fill the gap
Every other position prints a space, so the gap expands row by row, forming a V.
Duplicate-number V
Rows 2..5 show the same number twice.
Variation — User Input Version
Read the number of rows using scanf().
#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 <= 0without validation
Key Takeaways
Two diagonals are printed using conditional checks.
Rows 2..n print the number twice (duplicate effect).
Starting the right loop at 2 keeps the first row single.
Nested loops over positions lead to O(n²) time.
❓ Frequently Asked Questions
k=2, so row 1 has no right-side match.k=1 instead of 2.Explore More C Number Patterns!
Practice diagonal-based patterns and spacing tricks.
This is another “coordinate printing” pattern: you print only when a cell matches a condition like i == j.
12 people found this page helpful
