Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

Double Diamond Star Pattern in C

Diamond Star Pattern in C

Diamond Star Pattern in C

This program prints a diamond shaped star pattern using nested loops and conditional statements in C.

πŸ” C Program Code:

#include <stdio.h>

int main() {
    int num;
    printf("Enter the number:\n");
    scanf("%d", &num);

    // Upper part of diamond
    for(int i = 0; i < num; i++) {
        for(int j = 0; j < (2 * num); j++) {
            if(i + j <= num - 1) {
                printf("*");
            } else {
                printf(" ");
            }
            if(i + num <= j) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    // Lower part of diamond
    for(int i = 0; i < num; i++) {
        for(int j = 0; j < (2 * num); j++) {
            if(i >= j) {
                printf("*");
            } else {
                printf(" ");
            }
            if(i >= ((2 * num) - 1) - j) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

πŸ“Œ How It Works:

  • The program takes an integer input num which controls the size of the diamond.
  • The first pair of nested loops prints the upper half of the diamond:
    • Stars are printed on the left and right edges based on the conditions i + j <= num - 1 and i + num <= j.
    • Spaces fill the middle region, creating a hollow diamond shape.
  • The second pair of nested loops prints the lower half of the diamond:
    • Stars print based on conditions i >= j and i >= (2*num - 1) - j forming the symmetrical bottom.
  • Together, these loops form a complete diamond star pattern.

πŸ’» Sample Output for num=7:

* * * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *

Double Diamond Star Pattern in C

Double Diamond Star Pattern in C

This program prints a double diamond pattern of stars using nested loops in C.

πŸ” C Program Code:

#include <stdio.h>

int main() {
    int n;
    printf("Enter the number:\n");
    scanf("%d", &n);

    // Upper part of pattern
    for (int i = 0; i < n; i++) {
        // Left stars
        for (int j = 0; j < n - i; j++) {
            printf("* ");
        }
        // Spaces in middle
        for (int j = 0; j < 2 * i; j++) {
            printf("  ");
        }
        // Right stars
        for (int j = 0; j < n - i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    // Lower part of pattern
    for (int i = 0; i < n; i++) {
        // Left stars
        for (int j = 0; j <= i; j++) {
            printf("* ");
        }
        // Spaces in middle
        for (int j = 0; j < 2 * (n - i - 1); j++) {
            printf("  ");
        }
        // Right stars
        for (int j = 0; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

πŸ“Œ How It Works:

  • The first for loop prints the upper half of the double diamond.
  • It prints decreasing stars on the sides and increasing spaces in the center.
  • The second for loop prints the lower half with increasing stars on the sides and decreasing spaces in the center.
  • This creates a symmetric double diamond star pattern.

πŸ’» Sample Output for n=7:

* * * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *

Comments

Popular Posts

πŸŒ™