CREATE OWN LIBRARY

Right Triangle Number Pattern2

Back to Programming

Description

Right Triangle Number Pattern

The number of lines is taken as input. The number of lines of the pattern is 1 more than the input. The value of the first line is 0. The numbers of lines are calculated in the reversed order. From the next line, the values are printed from i to n, then 0 is printed, and then again the values are printed in reversed order i.e. from n to i.

Algorithm

INPUT: Number of lines
OUTPUT: the aforesaid pattern
PROCESS:
Step 1: [taking the input]
	Read n [number of lines]
Step 2: [printing the pattern]
	For i=n+1 to 1 repeat
		If i=n+1 then
			Print 0
		Else
			For j=i to n repeat
				Print j
			[End of ‘for’ loop]
			Print 0
			For j=n to i repeat
				Print j
			[End of ‘for’ loop]
		[End of ‘if]
	[End of ‘for’ loop]
Step 3: Stop.  

Code

Time Complexity:

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

                {              if(i==n+1)

                                                printf("0");

                                else

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

                                                                printf("%d",j);

                                                printf("0");

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

                                                                printf("%d",j);

                                }

                                printf("\n");      }

 

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