CREATE OWN LIBRARY

Number Pattern 6

Back to Programming

Description

Number Pattern: 

Here the number of lines is taken as input. For each line i the number of elements printed is n(number of lines). The number of lines is counted in reversed order. For each i, the values are printed from n to i+1, and then i times the value of i is printed.

Algorithm

INPUT: Number of lines
OUTPUT: the aforesaid pattern
PROCESS: 
Step 1: read n [the number of lines]
Step 2: [printing the pattern]
	For i=n to 1 repeat
		For j=n to i+1 repeat
			Print j
		[End of ‘for’ loop]
		For k=1 to i repeat
			Print i
		[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=n;j>i;j--)--------------------- n-i+1

                                                printf("%d ",j);

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

                                                printf("%d ",i);

                                printf("\n");

                }

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