C Program to Print Diamond Pattern with Numbers

Introduction

A diamond pattern with numbers consists of a symmetric arrangement of numbers in the shape of a diamond. The pattern increases in numbers in the upper half and decreases in the lower half. This exercise helps in understanding nested loops and controlling output with numbers in a specific pattern.

Problem Statement

Create a C program that:

  • Accepts the number of rows for the diamond pattern.
  • Prints a diamond-shaped pattern with numbers.

Example:

  • Input: rows = 5
  • Output:
        1
       1 2
      1 2 3
     1 2 3 4
    1 2 3 4 5
     1 2 3 4
      1 2 3
       1 2
        1
    

Solution Steps

  1. Input the Number of Rows: The number of rows defines the height of the diamond.
  2. Use Nested Loops: The outer loops control the rows, and the inner loops control printing the numbers and spaces for both the upper and lower parts of the diamond.
  3. Display the Diamond Pattern: Print numbers in increasing order for the upper part and decreasing order for the lower part, with spaces to align the numbers in a diamond shape.

C Program

#include <stdio.h>

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

    // Step 1: Accept the number of rows for the diamond pattern
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Step 2: Print the upper part of the diamond
    for (i = 1; i <= rows; i++) {
        // Print spaces for alignment
        for (j = i; j < rows; j++) {
            printf(" ");
        }
        // Print numbers in increasing order
        for (j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }

    // Step 3: Print the lower part of the diamond
    for (i = rows - 1; i >= 1; i--) {
        // Print spaces for alignment
        for (j = rows; j > i; j--) {
            printf(" ");
        }
        // Print numbers in increasing order
        for (j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }

    return 0;
}

Explanation

Step 1: Input Number of Rows

  • The program begins by asking the user to input the number of rows, which determines the height of the diamond pattern.

Step 2: Print the Upper Part of the Diamond

  • The outer loop controls the rows for the upper half of the diamond.
  • The first inner loop prints spaces to align the numbers in the center.
  • The second inner loop prints the numbers in increasing order for each row.

Step 3: Print the Lower Part of the Diamond

  • The second outer loop controls the rows for the lower half of the diamond.
  • Again, spaces are printed for alignment, and numbers are printed in increasing order to form the lower part of the diamond.

Output Example

For rows = 5, the output will be:

    1
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5
 1 2 3 4
  1 2 3
   1 2
    1

For rows = 4, the output will be:

   1
  1 2
 1 2 3
1 2 3 4
 1 2 3
  1 2
   1

Conclusion

This C program prints a diamond pattern with numbers. The program uses nested loops to print the upper and lower parts of the diamond, with spaces for alignment and numbers in increasing order. This exercise is useful for practicing loop control and formatting output in C programming.

Comments