FOR FREE MATERIALS

Number Pattern 14

Back to Programming

Description

Number Pattern: 

Here to print the pattern the number of lines is taken as input. For each line, the value is first printed in descending order and then in ascending order. In between this, spaces are printed.

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=n to i repeat
			Print j
		[End of ‘for’ loop]
		For k=j to 2 repeat
			Print " "
		[End of ‘for’ loop]
		For k=1 to j-1 repeat
			Print " "
		[End of ‘for’ loop]
		For j=i to n+1 repeat
			Print j
		[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=n;j>=i;j--)--------------------------------------------- n-i+1

                                                printf("%d",j);

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

                                                printf(" ");

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

                                                printf(" ");

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

                                                printf("%d",j);

                                printf("\n");

                }

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