FOR FREE CONTENT

Palindrome Character Pattern

Back to Programming

Description

Palindrome Character 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 in ascending order and then in descending order. Here the characters are printed instead of the numeric values.

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 charater(j+64)
		[End of ‘for’ loop]
		For k=j-2 to 1 repeat
			Print character(k+64)
		[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("%c ",(char)j+64);

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

                                                printf("%c ",(char)k+64);

                                printf("\n");

                }

 

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

Contributed by