Ex: Write a C Program to print swastika star pattern. How to write a C Program to print swastika star pattern. C Program to print swastika star pattern.
Input from user:
Enter the number:
5
Expected output:
Step by Step logic of the given program:
1. Accept input number from user declare variable say no.
2. Run outer for loop from 1 to (no*2-1):
for(i=1;i<=no*2-1;i++)
3. Run inner for loop from 1 to (no*2-1):
for(j=1;j<=no*2-1;j++)
4. Use if statement for add conditions to print stars:
if(i==no||j==no||i==1&&j>no||i<no&&j==1||i>no&&j==no*2-1||i==no*2-1&&j<no)
{
printf(" *");
}
5. Otherwise print spaces. For next row print new line(\n).
C Program to Print Swastika Star Pattern:
#include<stdio.h>
void main()
{
/* Declare variables*/
int i,j,no;
printf("Enter the number:\n");
scanf("%d",&no);
/* Run loops*/
for(i=1;i<=no*2-1;i++)
{
for(j=1;j<=no*2-1;j++)
{
/* Conditions for print stars*/
if(i==no||j==no||i==1&&j>no||i<no&&j==1||i>no&&j==no*2-1||i==no*2-1&&j<no)
{
printf(" *");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
Above Program shows the following output:
No comments:
Post a Comment
If you have any doubts, please discuss here...👇