FOR FREE CONTENT

Number Pattern 18

Back to Programming

Description

Number Pattern :

The number of lines is taken as input. For each line, the values are incremented and the values are the square of consecutive integers. Three for loops are used to print the pattern. The outer for loop is used to count the number of lines. The two inner loops are used to print the spaces and elements of each line.

Algorithm

INPUT: The number of lines
OUTPUT: The aforesaid pattern
PROCESS: 
Step 1: [taking the input]
	Read n [number of lines]
Step 2: [printing the pattern]
	Set p<-1
	Set q<-1
	For i=1 to n repeat
		For k=1 to n-i repeat
			Print “ “
		[End of ‘for’ loop]
		For j=1 to i repeat
			Print p2
			Set p<-p+1
		[End of ‘for’ loop]
		Set q<-q+2
	[End of ‘for’ loop]
Step 3: Stop.

Code

Time Complexity:

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

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

                                                printf("  ");

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

                                {              printf("%d ",(int)pow(p,2));

                                                p=p+1;     }

                                q=q+2;

                                printf("\n");       }

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