Program 4
Write a C Program to display a
Number Pattern by reading the number of rows as input.
The pattern creates a
symmetric pyramid where each row contains numbers from
1 to row number and then back down to 1, creating a beautiful diamond-like structure.
Pattern Structure (for n=4):
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
Algorithm
- Initialize: Start the program and declare variables n, i, j
- Input Rows: Read the number of rows (n) from user
- Outer Loop: For each row i from 1 to n
- Print Spaces: Print (n-i) spaces for proper alignment
- Ascending Numbers: Print numbers from 1 to i with spaces
- Descending Numbers: Print numbers from (i-1) down to 1 with spaces
- New Line: Move to next line after completing each row
- Terminate: Stop the program after all rows are printed
Flowchart
START
→
Input n
→
i = 1 to n
→
Print Spaces
→
Print 1 to i
→
Print i-1 to 1
→
New Line
→
STOP
Code
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
// Print spaces
for (j = 1; j <= n - i; j++) {
printf(" ");
}
// Print increasing numbers
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
// Print decreasing numbers
for (j = i - 1; j >= 1; j--) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Replay !
Share Your Thoughts