Butterfly Star Pattern
For printing the pattern the number of line is taken as input. One extra variable is taken here to count the number of stars and the number of spaces for each line. for printing this pattern the number of lines should be even. So after taking the number of lines as input a checking is done. If it is even, then only the pattern can be printed else an error message is shown.
INPUT: number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: [taking the input]
Read n [number of lines]
Step 2: [printing the pattern]
Set sp<-n-2
If n mod 2=0 then
For i=1 to n/2 repeat
For j=1 to i repeat
Print "*"
[End of ‘for’ loop]
For k=1 to sp repeat
Print “ “
[End of ‘for’ loop]
For j=1 to i repeat
Print "*"
[End of ‘for’ loop]
Move to the next line
Set sp<-sp-2
[End of ‘for’ loop]
Set sp<-sp+2
For i=n/2 to 1 repeat
For j=1 to i repeat
Print "*"
[End of ‘for’ loop]
For k=1 to sp repeat
Print “ “
[End of ‘for’ loop]
For j=1 to i repeat
Print "*"
[End of ‘for’ loop]
Move to the next line
Set sp<-sp+2
[End of ‘for’ loop]
else
print “Please give correct input(no. of lines should be even)"
[End of ‘if]
Step3: Stop.
for(i=1;i<=n/2;i++)--------------------------------- n/2
{ for(j=1;j<=i;j++)--------------------------- i
printf("*");
for(k=1;k<=sp;k++)----------------------- sp
printf(" ");
for(j=1;j<=i;j++)---------------------------- i
printf("*");
printf("\n");
sp-=2;
}
//printing the second half of the pattern where the number of stars
//are decreasing
sp=sp+2;
for(i=n/2;i>=1;i--)------------------------------------- n/2
{ for(j=1;j<=i;j++)------------------------------ i
printf("*");
for(k=1;k<=sp;k++)-------------------------- sp
printf(" ");
for(j=1;j<=i;j++)-------------------------------- i
printf("*");
printf("\n");
sp+=2; }
The complexity is: O((n/2)*(2*i+sp))=O().