FOR FREE YEAR SOLVED

Palindrome Number Pattern

Back to Programming

Description

Palindrome Number Pattern:

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 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(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*(i+j-2))=O(n2)