FOR FREE MATERIALS

Number Pattern 4

Back to Programming

Description

Number Pattern:

The aforesaid pattern is printed by taking the number of lines as input. For each line, the elements are printed from i to n-1 and then the value of n is printed i times. Three for loops are used here to print the aforesaid pattern. The line number is calculated in reversed order

Algorithm

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

                                                printf("%d ",k);

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

                                                printf("%d ",n);

                                printf("\n");

                }

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