15th tutorial for right angle triangle patterns



In last tutorial we have created a logic to print numbers in pattern. In this tutorial also we have different logic to print numbers in particular manner in right angle triangle only.




Program can be made of the pattern looks like




As you can notice from the figure that starting from odd number in first line, then even numbers in second line, again odd numbers in third line and so on. So this pattern can be programmed as follows:



Now here the logic is that as we divide the number with 2 and accordingly values are set. Here initialization is from 1, so dividing 1 by 2 we get remainder 1 so we set k variable with 1 and printing it in first line. Then i is incremented to 2 and dividing 2 by 2 we get 0 so k is set to 2 and in second line 2 and (k+=2) i.e. increment k with 2 we will have 4 so 2 4 in second line. Now i will be incremented to 3 and same procedure will be carried out for the maximum number of lines entered by the user.

Here in inner loop there are two incrementing statement i.e. j++ to increment the line and k+=2 to increment the value of k to have series of odd or even numbers as necessary.

Outer loop i.e. i loop remains same starting from 1 till maximum number n.

As we have to print integer numbers %d will be use.


{ 0 comments ... read them below or add one }

Post a Comment