FOR FREE MATERIALS

Palindrome Pyramid Number Pattern3

Back to Programming

Description

Palindrome Pyramid Number Pattern:

The number of lines is taken as input. For each line, the values are incremented, and then again it is decremented. Three for loops are used to print the pattern. The outer for loop is used to count the number of lines. The two inner loops are used to print the elements of each line.

Algorithm

INPUT: The number of lines
OUTPUT: The aforesaid pattern
PROCESS: 
Step 1: [taking the input]
	Read n [number of lines]
Step 2: [printing the pattern]
	Set p<-1
	Set q<-1
	For i=1 to n repeat
		Set p<-q
		For k=1 to n-i repeat
			Print “ “
		[End of ‘for’ loop]
		For j=1 to i repeat
			Print p
			Set p<-p+1
		[End of ‘for’ loop]
		Set p<-p-2
		For j=1 to i-1 repeat
			Print p
			Set p<-p-1
		[End of ‘for’ loop]
		Set q<-q+i
	[End of ‘for’ loop]
Step 3: Stop.

Code

Time Complexity:

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

                {             p=q;

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

                                                printf("  ");

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

                                {              printf("%d ",p);

                                                p++;        }

                                p-=2;

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

                                {              printf("%d ",p);

                                                p--;      }

                                q=q+i;

                                printf("\n");

                }

 

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