FOR FREE CONTENT

Number Pattern 1

Back to Programming

Description

Number Pattern 1:

Here in this pattern printing, the number of lines of the pattern is taken as the input. Two for loops are used to display the pattern. The first for loop (the outer for loop) is used to count the line number so the loop is from 1 to n. for ith line the numbers are printed from i and the next element is increased by 5 and so on.

Algorithm

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

                                                printf("%d ",k);

                                printf("\n");

                }

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