FOR FREE MATERIALS

Half Right Diamond Number Pattern 2

Back to Programming

Description

Half Right Diamond Number Pattern:

Here for printing the pattern the number of lines is taken as inputs. The pattern is divided into two halves. In the first half the number of elements is increased and in the 2nd half the number of elements is decreased. For ith line the difference between two consecutive elements is i. the number of lines of this pattern should be odd.

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
	For i=1 to n/2+1 repeat
		Set k<-i
		For j=1 to i repeat 
			Print k
			Set k<-k+i
		Move to the next line
	[End of ‘for’ loop]
	Set e<-i-2
	For i=n/2+2 to n repeat
		Set k<-i
		For j=1 to e repeat 
			Print k
			Set k<-k+i
		[End of ‘for’ loop]
		Move to the next line	
		Set e<-e-1		
	[End of ‘for’ loop]
	else
		print "The pattern can be printed only if the number of lines is odd"
[End of ‘if’]
Step 3. Stop.

Code

Time Complexity:

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

                                {              for(j=1,k=i;j<=i;j++,k+=i)----------------------- i

                                                {     printf("%d ",k);  

                                                }

                                                printf("\n"); 

                                }

                                e=i-2;

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

                                {              for(j=1,k=i;j<=e;j++,k+=i)------------------------ e

                                                {    printf("%d ",k);

                                                }

                                                printf("\n");       

                                                e--;                         

                                }

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