C Program To Print Swastik Pattern

C Program To Print Swastik Pattern

C Program To Print Swastik Pattern

Example 


#include<stdio.h>
int main()
{
     
int a,b,n;
     
printf("Enter Swastik Size(n):");

     scanf("%d",&n);

     printf("* ");
   
     
for(a=0; a<n-2; a++)

           printf("  ");
   
     
for(a=0; a<n; a++)  
           
printf("* ");
   
     
printf("\n");
   
     
for(b=0; b<n-2; b++)
     {
           
printf("* ");
           
for(a=0; a<n-2; a++)
               
printf("  ");
           
printf("* \n");
     }
   
     
for(a=0; a<n*2-1; a++)    
           
printf("* ");
     
printf("\n");
   
     
for(b=0; b<n-2; b++)
     {
           
for(a=0; a<=n-2; a++)
               
printf("  ");

           printf("* ");

           for(a=0; a<n-2; a++)

                printf("  ");

           printf("* \n");
     }

     for(a=0; a<n; a++)  

           printf("* ");

     for(a=0; a<n-2; a++)

           printf("  ");

     printf("* ");  

     return 0;
}


C Program To Print Swastik Pattern

Explain Program

Step :1

Header

#include <stdio.h>
  •  standard input-output library use functions

Step :2

Main Function Declaration

int main() {

  • The entry point of the program. Execution starts 

Step :3

int a, b, n;

Declare loop variables a, b, and n where:

n: The size of the Swastik.

 a, b: Loop counters for row and column logic.

Step :4

printf("Enter Swastik Size(n):");

scanf("%d", &n);

 user to enter the size of the Swastik symbol.

Input is stored in variable n.

Step :5

printf("* ");

for (a = 0; a < n - 2; a++)

     printf("  ");

for(a = 0; a < n; a++) 

    printf("* ");

printf("\n");

 Explanation:

 prints the top part of the Swastik 

Let's say n = 7, this will print:

Step :6

for(b = 0; b < n - 2; b++)

{

    printf("* ");

    for(a = 0 ; a < n - 2; a++)

        printf("  ");

    printf("* \n");

}

Step :7

for(a = 0 ; a < n * 2 - 1; a++)   

    printf("* ");

printf("\n");

Explanation:

 center horizontal bar of the Swastik.

 

n*2  - 1 stars are print in one row.

 n = 7, prints 13 stars (i.e., 2n - 1 = 13):

Step :8

for(b = 0; b < n - 2; b++)

{

    for(a = 0; a <= n - 2; a++)

        printf("  ");

    printf("* ");

    for(a =  0 ; a < n - 2; a++)

        printf("  ");

    printf("* \n");

}

Explanation:

Prints (n-2) rows again.

Step :9

for(a = 0; a < n; a++) 

    printf("* ");

for(a = 0; a < n - 2; a++)

    printf("  ");

printf("* "); 

* Explanation:

Prints the bottom part of the Swastik.

For n = 7, outputs:

C Program To Print Swastik Pattern

 Program Output 


                          

Related Post :-


triangle patterns in php program

Comments

Popular posts from this blog

how to add background image in html.

HTML pre tag

How to insert image in html

Html anchor tag

Inline css in html