FOR FREE CONTENT

Half Right Diamond Number Pattern 6

Back to Programming

Description

Half Right Diamond Number Pattern:

The number of lines is taken as input. The number of lines of this pattern should be odd. Pattern is divided into two halves, in the first half the number of elements is increased and in the second half, the number of elements is decreased. In the first line and in the last line, only one star is printed, otherwise, one star is printed before the elements and one star is printed after the elements.

Algorithm

INPUT: The number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: Read n [number of lines]
Step 2: [printing the pattern]
	If n mod 2≠0 then
		Set d<-n/2+1
		For i=0 to d-1 repeat
			If i=0 then
				Print *
			Else
				Print *
				For j=1 to i repeat
					Print j
				[End of ‘for’ loop]
				For k=j-2 to 1 repeat
					Print k
				[End of ‘for’ loop]
				Print *
			[End of ‘if’]
			Move to the next line
		[End of ‘for’ loop]
		For i=i-2 to 0 repeat
			If i=0 then
				Print *
			Else
				Print *
				For j=1 to i repeat
					Print j
				[End of ‘for’ loop]
				For k=j-2 to 1 repeat
					Print k
				[End of ‘for’ loop]
				Print *
			[End of ‘if’]
			Move to the next line
		[End of ‘for’ loop]
	else
		print "the number of lines should be odd"
	[End of ‘for’ loop]
Step 3: Stop.

Code

Time Complexity:

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

                                {              if(i==0)

                                                                printf("*");

                                                else

                                                {              printf("* ");

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

                                                                                printf("%d ",j);

                                                                for(k=j-2;k>=1;k--)---------------- j-2

                                                                                printf("%d ",k);

                                                                printf("*");   }

                                                printf("\n");

                                }

                                for(i=i-2;i>=0;i--)-------------------------------------------- n/2

                                {              if(i==0)

                                                                printf("*");

                                                else

                                                {              printf("* ");

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

                                                                                printf("%d ",j);

                                                                for(k=j-2;k>=1;k--)------------------- j-2

                                                                                printf("%d ",k);

                                                                printf("*");

                                                }

                                                printf("\n");

                                }

The complexity is: O(((n/2+1)*(i+j-2))+((n/2)*(i+j-2)))=O(n2)