FOR FREE MATERIALS

Palindrome Number Pyramid Pattern1

Back to Programming

Description

Palindrome Number Pyramid:

The number of lines is taken as input. Three for loops are used to print the pattern. For each line i, at first, the elements are printed from 1 to i and then from i-1 to 1.

Algorithm

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

Code

Time Complexity:

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

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

                                                printf("  ");

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

                                                printf("%d ",j);

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

                                                printf("%d ",k);

                                printf("\n");

                }

 

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