FOR FREE YEAR SOLVED

Number Pattern 15

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

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

                                                printf("%d",j);

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

                                                printf(" ");

                                for(k=n;k>i;k--)------------------------------------ n-i

                                                printf(" ");

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

                                                printf("%d",j);

                                printf("\n");

                }

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