FOR FREE MATERIALS

Palindrome Pyramid Number Pattern2

Back to Programming

Description

Palindrome Pyramid Number Pattern2:

The number of lines is taken as input. The elements of the pattern are in the power of 2. For each line first the elements are incremented and then it is decremented.

Algorithm

INPUT: The number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: Read n [number of lines]
Step 2: [printing the pattern]
	Set d<-n-1
	For i=1 to n repeat
		For j=1 to d repeat
			Print “  ”
		[End of ‘for’ loop]
		For k=0 to i-1 repeat
			Print 2k
		[End of ‘for’ loop]
		For k=k-1 to 0 repeat
			Print 2k
		[End of ‘for’ loop]
		Set d<-d-1
		Move to the next line
	[End of ‘for’ loop]
Step 3: Stop.

Code

Time Complexity:

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

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

                                                printf("  ");

                                for(k=0;k<i;k++)--------------------------------------- i

                                                printf("%d ",(int)pow(2,k));

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

                                                printf("%d ",(int)pow(2,k));

                                printf("\n");

                                d--;

                }

 

The complexity is: O(n*(d+i+k-1))=O(n2)