Diamond Number-Star Pattern (Sandwich Inverted) - C Program
Understanding the Problem
The goal is to print a diamond-shaped pattern where numbers are printed in decreasing order in the upper half, followed by a repeated minimum row, and then increasing order in the lower half. Each number is followed by a star except for the last number in each row.
Pattern Explanation
The pattern consists of three parts: the upper half, the repeated minimum row, and the lower half.
4*4*4*4 3*3*3 2*2 1 1 2*2 3*3*3 4*4*4*4
Observations:
- The pattern consists of 7 rows.
- Numbers decrement from 4 to 1 in the upper half.
- The minimum row (1) is repeated once.
- Each number is followed by a '*', except the last number in each row.
- The lower half mirrors the upper half, incrementing from 1 to 4.
Algorithm
The pattern follows these steps:
- Initialize a variable to determine the maximum number (N).
- Loop through rows from N down to 1 to print the upper half of the diamond.
- For each row, loop through columns (up to the current row number) and print the current row number.
- Print the number followed by '*' except for the last number in the row.
- After completing the upper half, print the minimum row (1) again.
- Loop through rows from 1 to N to print the lower half of the diamond.
- Repeat steps 3 and 4 for the lower half.
- Move to the next line after printing each row.
Method 1: Using Nested Loops
This method uses two loops to print the pattern.
#include <stdio.h> int main() { int n = 4; // Maximum number for the diamond pattern // Loop to print the upper half of the diamond for (int i = n; i >= 1; i--) { // Loop for each row for (int j = 1; j <= i; j++) { // Loop for each column in the current row printf("%d", i); // Print the current row number if (j < i) printf("*"); // Print '*' if not the last number in the row } printf("\n"); // Move to the next line after each row } // Print the minimum row again for (int j = 1; j <= 1; j++) { // Loop for the minimum row printf("1"); // Print the minimum number if (j < 1) printf("*"); // Print '*' if not the last number in the row } printf("\n"); // Move to the next line after the minimum row // Loop to print the lower half of the diamond for (int i = 2; i <= n; i++) { // Loop for each row (starting from 2 to avoid duplicate 1) for (int j = 1; j <= i; j++) { // Loop for each column in the current row printf("%d", i); // Print the current row number if (j < i) printf("*"); // Print '*' if not the last number in the row } printf("\n"); // Move to the next line after each row } return 0; }
Explanation:
- The first nested loop prints the upper half of the diamond, decrementing from n to 1.
- The middle loop prints the minimum row (1) again to create the "sandwich" effect.
- The last nested loop prints the lower half of the diamond, incrementing from 2 to n (starting from 2 to avoid duplicate 1).
- Each number is followed by a '*' except for the last number in each row.
Output:
4*4*4*4 3*3*3 2*2 1 1 2*2 3*3*3 4*4*4*4
Method 2: Using Functions
This approach modularizes the code by using functions for better readability and reusability.
#include <stdio.h> void printRow(int num, int count) { for (int j = 1; j <= count; j++) { printf("%d", num); if (j < count) printf("*"); } printf("\n"); } void printInvertedDiamond(int n) { // Upper half for (int i = n; i >= 1; i--) { printRow(i, i); } // Middle repeated row printRow(1, 1); // Lower half (starting from 2 to avoid duplicate 1) for (int i = 2; i <= n; i++) { printRow(i, i); } } int main() { int n = 4; printInvertedDiamond(n); return 0; }
Advantages:
- More modular and readable code
- Easier to maintain and modify
- Reusable function for printing rows
- Clear separation of concerns
Comparison with Regular Sandwich Pattern
Key differences between the regular and inverted sandwich patterns:
Aspect | Regular Sandwich | Inverted Sandwich |
---|---|---|
Upper Half | Numbers increment (1 to n) | Numbers decrement (n to 1) |
Middle Row | Maximum number (n) repeated | Minimum number (1) repeated |
Lower Half | Numbers decrement (n-1 to 1) | Numbers increment (2 to n) |
Visual Appearance | Pointy at top and bottom | Pointy in the middle |