CREATE OWN LIBRARY

Diamond inside Rectangle Star Pattern

Back to Programming

Description

A diamond inside Rectangle Star Pattern

For printing the pattern the number of line is taken as input. Two extra variables are 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.

Algorithm

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 st<-1
	Set sp<-n/2
	If n mod 2=0 then
For i=1 to n/2 repeat
			For j=1 to sp repeat
				Print "*"
			[End of ‘for’ loop]
			For k=1 to st repeat
				Print “ “
			[End of ‘for’ loop]
			For j=1 to sp repeat
				Print "*"
			[End of ‘for’ loop]
			Move to the next line
			Set sp<-sp-1
			Set st<-st+2
		[End of ‘for’ loop]
		Set sp<-sp+1
		Set st<-st-2
		For i=n/2+1 to n repeat
			For j=1 to sp repeat
				Print "*"
			[End of ‘for’ loop]
			For k=1 to st repeat
				Print “ “
			[End of ‘for’ loop]
			For j=1 to sp repeat
				Print "*"
			[End of ‘for’ loop]
			Move to the next line
			Set sp<-sp+1
			Set st<-st-2			
		[End of ‘for’ loop]
	else
		print “Please give correct input(no. of lines should be even)"
	[End of ‘if]
Step3: Stop.

Code

Time Complexity:

for(i=1;i<=n/2;i++)----------------------- n/2

                                {             for(j=1;j<=sp;j++)--------------- sp

                                                                printf("*");

                                                for(k=1;k<=st;k++)-------------- st

                                                                printf(" ");

                                                for(j=1;j<=sp;j++)---------------- sp

                                                                printf("*");

                                                printf("\n");

                                                sp--;

                                                st+=2;   }

                                //printing the second half of the pattern where the number of stars

                                //are decreasing

                                sp=sp+1;

                                st=st-2;

                                for(i=n/2+1;i<=n;i++)----------------------- n/2

                                {              for(j=1;j<=sp;j++)------------------ sp

                                                                printf("*");

                                                for(k=1;k<=st;k++)----------------- st

                                                                printf(" ");                           

                                                for(j=1;j<=sp;j++)------------------- sp

                                                                printf("*");

                                                printf("\n");

                                                sp++;

                                                st-=2;    }

 

The time complexity is: O((n/2)*(2*sp+st))=O(n2)