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() { ...

Diamond pattern in C

C Program: Diamond Star Pattern

πŸ”· C Program: Diamond Star Pattern

#include<stdio.h>
int main()
{
    int num;
    printf("Enter the number:\n");
    scanf("%d",&num);

    // Upper Half
    for(int i=1; i<=num; i++)
    {
        for(int k=num; k>=i; k--)
        {
            printf(" ");
        }
        for(int j=i; j>=1; j--)
        {
            printf("* ");
        }
        printf("\n");
    }

    // Lower Half
    for(int i=num; i>=1; i--)
    {
        for(int k=num; k>=i; k--)
        {
            printf(" ");
        }
        for(int j=1; j<=i; j++)
        {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
  

πŸ“˜ Explanation:

This C program prints a full diamond star pattern. It first constructs the top half of the diamond using an upright pyramid of stars with leading spaces for alignment. The bottom half is constructed using an inverted pyramid.

- The outer loops run for `num` lines each for the top and bottom halves. - The inner loops handle spacing and star printing. - Spacing ensures the stars are centered, forming a symmetric diamond shape.

πŸ” Sample Output:

Enter the number:
4
    * 
   * * 
  * * * 
 * * * * 
 * * * * 
  * * * 
   * * 
    * 
    

🏷️ Keywords:

C star pattern, diamond pattern in C, full diamond program, half pyramid in C, inverted pyramid, star shape printing in C, C loop practice

Comments

Popular Posts

πŸŒ™