Skip to content
Related Articles

Related Articles

Printing the Triangle Pattern using last term N
  • Last Updated : 26 Mar, 2021

Given a number N which represents the last term of the Triangle Pattern. The task is to print the Triangle Pattern from 1 to N, such that each row is complete.
Triangle Pattern is given as: 
 

   1
  2 3
 4 5 6
7 8 9 10
.
.

Examples: 
 

Input: N = 3
Output:
 1
2 3

Input: N = 7
Output:
   1
  2 3
 4 5 6

7 will not be printed as 
it would result in an incomplete row

 

Approach: 
 

  • Find the number of complete rows from the given last term N. 
    • As: 
      For Max Height = 1, the last term would be 1 
      For Max Height = 2, the last term would be 3 
      For Max Height = 3, the last term would be 6
    • So the last term forms a pattern: 1, 3, 6, 10, 15,…
    • Therefore, the n-th term of series 1, 3, 6, 10, 15,… 
      A(n) = 1 + 2 + 3 + 4… + (n – 1) + n 
      = n(n + 1) / 2
      i.e A(n) is the sum of First n natural numbers.
    • So in 
       
A(n) = n(n + 1) / 2
A(n) represents the last term (as per our problem),
and n represents the max height of the Triangle
  •  
  • Hence this can be seen as: 
     
Last term = height (height + 1) / 2
  •  
  • Therefore, 
     
height = (-1 + sqrt(1 + 8*lastTerm)) / 2
  •  
  • After finding the max height, the triangle pattern can be easily printed.

Below is the implementation of the above approach:
 

C++




// C++ code for printing the
// Triangle Pattern using last term N
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to demonstrate printing pattern
void triangle(int n)
{
    // number of spaces
    int k = 2 * n - 2;
 
    // character to be printed
    int ch = 1;
 
    // outer loop to handle number of rows
    // n in this case
    for (int i = 0; i < n; i++) {
 
        // inner loop to handle number spaces
        // values changing acc. to requirement
        for (int j = 0; j < k; j++)
            cout << " ";
 
        // decrementing k after each loop
        k = k - 1;
 
        // inner loop to handle number of columns
        // values changing acc. to outer loop
        for (int j = 0; j <= i; j++) {
            // printing stars
            cout << ch++ << " ";
        }
 
        // ending line after each row
        cout << endl;
    }
}
 
// Function to find the max height
// or the number of lines
// in the triangle pattern
int maxHeight(int n)
{
    return (((int)sqrt(1 + 8.0 * n)) - 1) / 2;
}
 
// Driver Function
int main()
{
    int N = 9;
    triangle(maxHeight(N));
    return 0;
}

Java




// Java code for printing the
// Triangle Pattern using last term N
import java.util.*;
 
class GFG
{
 
// Function to demonstrate printing pattern
static void triangle(int n)
{
    // number of spaces
    int k = 2 * n - 2;
 
    // character to be printed
    int ch = 1;
 
    // outer loop to handle number of rows
    // n in this case
    for (int i = 0; i < n; i++)
    {
 
        // inner loop to handle number spaces
        // values changing acc. to requirement
        for (int j = 0; j < k; j++)
            System.out.print(" ");
 
        // decrementing k after each loop
        k = k - 1;
 
        // inner loop to handle number of columns
        // values changing acc. to outer loop
        for (int j = 0; j <= i; j++)
        {
            // printing stars
            System.out.print(ch++ + " ");
        }
 
        // ending line after each row
        System.out.println();
    }
}
 
// Function to find the max height
// or the number of lines
// in the triangle pattern
static int maxHeight(int n)
{
    return (((int)Math.sqrt(1 + 8.0 * n)) - 1) / 2;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 9;
    triangle(maxHeight(N));
}
}
 
// This code is contributed by PrinciRaj1992

Python3




# Python3 code for printing the
# Triangle Pattern using last term N
from math import sqrt
 
# Function to demonstrate printing pattern
def triangle(n) :
 
    # number of spaces
    k = 2 * n - 2;
 
    # character to be printed
    ch = 1;
 
    # outer loop to handle number of rows
    # n in this case
    for i in range(n) :
 
        # inner loop to handle number spaces
        # values changing acc. to requirement
        for j in range(k) :
            print(" ", end = "");
 
        # decrementing k after each loop
        k = k - 1;
 
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(i + 1) :
             
            # printing stars
            print(ch, end = " ");
            ch += 1;
             
        # ending line after each row
        print()
 
# Function to find the max height
# or the number of lines
# in the triangle pattern
def maxHeight(n) :
    ans = (sqrt(1 + 8.0 * n) - 1) // 2;
    return int(ans);
 
# Driver Code
if __name__ == "__main__" :
 
    N = 9;
    triangle(maxHeight(N));
     
# This code is contributed by AnkitRai01

C#




// C# code for printing the
// Triangle Pattern using last term N
using System;
     
class GFG
{
 
// Function to demonstrate printing pattern
static void triangle(int n)
{
    // number of spaces
    int k = 2 * n - 2;
 
    // character to be printed
    int ch = 1;
 
    // outer loop to handle number of rows
    // n in this case
    for (int i = 0; i < n; i++)
    {
 
        // inner loop to handle number spaces
        // values changing acc. to requirement
        for (int j = 0; j < k; j++)
            Console.Write(" ");
 
        // decrementing k after each loop
        k = k - 1;
 
        // inner loop to handle number of columns
        // values changing acc. to outer loop
        for (int j = 0; j <= i; j++)
        {
            // printing stars
            Console.Write(ch++ + " ");
        }
 
        // ending line after each row
        Console.WriteLine();
    }
}
 
// Function to find the max height
// or the number of lines
// in the triangle pattern
static int maxHeight(int n)
{
    return (((int)Math.Sqrt(1 + 8.0 * n)) - 1) / 2;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 9;
    triangle(maxHeight(N));
}
}
 
// This code is contributed by Princi Singh

Javascript




<script>
// Javascript code for printing the
// Triangle Pattern using last term N
 
// Function to demonstrate printing pattern
function triangle(n)
{
 
    // number of spaces
    var k = 2 * n - 2;
 
    // character to be printed
    var ch = 1;
 
    // outer loop to handle number of rows
    // n in this case
    for (var i = 0; i < n; i++) {
 
        // inner loop to handle number spaces
        // values changing acc. to requirement
        for (var j = 0; j < k; j++)
            document.write(" ");
 
        // decrementing k after each loop
        k = k - 1;
 
        // inner loop to handle number of columns
        // values changing acc. to outer loop
        for (var j = 0; j <= i; j++) {
            // printing stars
            document.write(ch++ + " ");
        }
 
        // ending line after each row
        document.write("<br>");
    }
}
 
// Function to find the max height
// or the number of lines
// in the triangle pattern
function maxHeight(n)
{
    return parseInt(((parseInt(Math.sqrt(1 + 8.0 * n))) - 1) / 2);
}
 
// Driver Function
var N = 9;
triangle(maxHeight(N));
 
// This code is contributed by noob2000.
</script>
Output: 
    1 
   2 3 
  4 5 6

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :