FOR FREE YEAR SOLVED

Right Triangle Number Pattern1

Back to Programming

Description

Right Triangle Number Pattern:

Here to print the pattern the number of lines is taken as inputs. Two for loops are used here to print the pattern. The outer for loop is used to count the number of lines so the loop is from 1 to n. the inner for loop is used to print the elements. For ith line the number of elements is i and the i  elements of the Fibonacci series are printed except the first two terms of the series i.e. 0 and 1.

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=1 to n repeat
		Set a<-0
		Set b<-1
		For j=1 to i repeat
			Set c<-a+b
			Print c
			Set a<-b
			Set b<-c
		[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

                {              a=0,b=1;

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

                                {

                                                c=a+b;

                                                printf("%d ",c);

                                                a=b;

                                                b=c;       

                                }

                                printf("\n");      }

 

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