Write a program to print pattern in C like below pattern

Pattern in C are asked in different examinations to test the candidate’s logic to solve the problems. Here some examples are given for your practice .

1 . Program to print hollow square pattern in C

Input & Output:

pattern in c

Source Code :

/******************************************************************************
Write a program in C to print hollow square pattern


*******************************************************************************/
#include<stdio.h>
 
int main(){
    int side, r, c;
     
    printf("Enter square side no : ");
    scanf("%d", &side);
     
    /* Loop for row */
    for(r = 0; r < side; r++){
     /* Loop for column */
        for(c = 0; c < side; c++){
            /* checking for left and right boundaries */
            if(r==0 || r==side-1 || c==0 || c==side-1)
                printf("*");
            else
                printf(" ");
        }
       printf("\n");
    }
    return 0;
}

2 . Program to print hollow Square / Rectangle pattern with diagonals using star (*) in C Language

Input and Output :

program to print hollow square/rectangle pattern with diagonals in c

Source Code :

#include<stdio.h>

void main()
{
    int num;

    printf("Enter number of asterisks side: ");

    scanf("%d", &num);

    int r, c;
    
    // r = row  and c = column

    for (r = 1; r<= num; r++)
    {
        for (c = 1; c <= num; c++)
        {
            // Conditions on which  pattern are going to print out 
            
            if
            (
                r == 1 ||  r == num ||
                c == 1 || c == num||
                r == c || c == (num - r + 1)
            ){
                printf("*");
            }else{
                printf(" ");
            }
        }

        printf("\n");
    }
}
Share This!

Leave a Reply

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