C (programming language)

C Program to Print Diamond Number Pattern

Introduction

This guide will walk you through creating a C program that generates a visually appealing diamond-shaped pattern composed of numbers. We’ll explore the logic, code structure, and key steps involved in crafting this pattern.

Understanding the Pattern

  • Shape: The pattern resembles a diamond, with a symmetrical structure.
  • Composition: Numbers are used to construct the diamond, arranged in a specific order.
  • Sections: The pattern can be divided into two halves: an upper half and a lower half, which are mirror images of each other.

Steps to Implement

  1. Include Necessary Header:

C

#include <stdio.h>
  1. Declare Variables:

C

int rows, i, j, num = 1;
  1. Get Input for Number of Rows:

C

printf("Enter the number of rows: ");
scanf("%d", &rows);
  1. Print the Upper Half of the Diamond:

C

for (i = 1; i <= rows; i++) {
    // Print spaces for left padding
    for (j = 1; j <= rows - i; j++) {
        printf(" ");
    }

    // Print numbers in increasing order
    for (j = 1; j <= i * 2 - 1; j++) {
        printf("%d", num);
        num++;
    }

    num--;  // Decrement num for the next row
    printf("\n");  // Move to the next line
}
  1. Print the Lower Half of the Diamond:

C

for (i = rows - 1; i >= 1; i--) {
    // Print spaces for left padding
    for (j = 1; j <= rows - i; j++) {
        printf(" ");
    }

    // Print numbers in decreasing order
    for (j = 1; j <= i * 2 - 1; j++) {
        printf("%d", num);
        num--;
    }

    num += 2;  // Increment num for the next row
    printf("\n");
}

Complete Example Code:

C

#include <stdio.h>

int main() {
    int rows, i, j, num = 1;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Upper Half
    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= rows - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= i * 2 - 1; j++) {
            printf("%d", num);
            num++;
        }
        num--;
        printf("\n");
    }

    // Lower Half
    for (i = rows - 1; i >= 1; i--) {
        for (j = 1; j <= rows - i; j++) {
            printf(" ");
        }
        for (j = 1; j <= i * 2 - 1; j++) {
            printf("%d", num);
            num--;
        }
        num += 2;
        printf("\n");
    }

    return 0;
}

Explanation

  1. The first for loop handles the upper half of the diamond, printing numbers in increasing order and using spaces for padding.
  2. The second for loop handles the lower half, printing numbers in decreasing order and also using spaces for padding.
  3. num is carefully incremented and decremented to control the sequence of numbers in each row.
  4. printf("\n") moves the cursor to the next line after each row.

Key Points to Remember

  • Nested loops are essential for creating the diamond’s shape and number arrangement.
  • The num variable plays a crucial role in managing the number sequence.
  • Adjust the rows variable to control the size of the diamond pattern.

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button