FOR FREE CONTENT

Star Full Diamond

Back to Programming

Description

Star Full Diamond

   

For printing the pattern, the number of lines is taken as input. The pattern is divided into two parts. In the first half, the number of stars are increased and in the 2nd part, the number of stars are decreased. 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 odd. So after taking the number of lines as input checking is done. If it is odd, 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+1
	If n mod 2≠0 then
For i=1 to n/2+1 repeat
			For j=1 to sp repeat
				Print " "
			[End of ‘for’ loop]
			For k=1 to st 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+2
		Set st<-st-4
		For i=n/2+2 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]
			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 odd)"
	[End of ‘if]
Step3: Stop.

Code

Time Complexity:

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

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

                                                                printf(" ");

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

                                                                printf("*");

                                                printf("\n");

                                                sp--;

                                                st+=2;         }

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

                                //are decreasing

                                sp=sp+2;

                                st=st-4;

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

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

                                                                printf(" ");

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

                                                                printf("*");

                                                printf("\n");

                                                sp++;

                                                st-=2;     }

                                 }

The time complexity of this program=O((n/2+1)*(sp+st)+ (n/2)*(sp+st))=O(n).