Open In App

Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides

Given N-sided polygon we need to find the total number of triangles formed by joining the vertices of the given polygon with exactly two sides being common and no side being common.
Examples: 
 

Input : N = 6 
Output : 6 2 
The image below is of a triangle forming inside a Hexagon by joining vertices as shown above. 
The triangle formed has two sides (AB and BC) common with that of a polygon. Similarly BC and 
CD can make one triangle. With this, we can say that there will be a total of 6 triangles possible 
having two sides common with that of a polygon. The second image of a hexagon, 
a triangle is formed with no side common with that of a polygon. 
There will be just 2 triangles possible, BFD and ACE. 
 



Triangle with two side common and no side common of the Hexagon

Number of triangles formed are 6 and 2 with two side common and with no side common respectively.
Input : N = 7 
Output : 7 7 
 

 



Approach : 
 

Note:To calculate the number of triangles having one side common with that of a polygon click here
Below is the implementation of the above approach: 
 




// C++ program to implement
// the above problem
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the number of triangles
void findTriangles(int n)
{
    int num = n;
 
    // print the number of triangles
    // having two side common
    cout << num << " ";
 
    // print the number of triangles
    // having no side common
    cout << num * (num - 4) * (num - 5) / 6;
}
 
// Driver code
int main()
{
    // initialize the number
    // of sides of a polygon
    int n;
    n = 6;
 
    findTriangles(n);
 
    return 0;
}




// Java program to implement
// the above problem
import java.io.*;
 
class GFG
{
 
 
// Function to find the number of triangles
static void findTriangles(int n)
{
    int num = n;
 
    // print the number of triangles
    // having two side common
    System.out.print( num + " ");
 
    // print the number of triangles
    // having no side common
    System.out.print( num * (num - 4) * (num - 5) / 6);
}
 
// Driver code
public static void main (String[] args)
{
    // initialize the number
    // of sides of a polygon
    int n;
    n = 6;
 
    findTriangles(n);
}
}
 
// This code is contributed by anuj_67..




# Python3 program to implement
# the above problem
 
# Function to find the number of triangles
def findTriangles(n):
    num = n
     
 
    # print the number of triangles
    # having two side common
    print(num, end = " ")
 
    # print the number of triangles
    # having no side common
    print(num * (num - 4) * (num - 5) // 6)
 
# Driver code
 
# initialize the number
# of sides of a polygon
n = 6;
 
findTriangles(n)
 
# This code is contributed by Mohit Kumar




// C# program to implement
// the above problem
using System;
 
class GFG
{
 
 
// Function to find the number of triangles
static void findTriangles(int n)
{
    int num = n;
 
    // print the number of triangles
    // having two side common
    Console.Write( num + " ");
 
    // print the number of triangles
    // having no side common
    Console.WriteLine( num * (num - 4) * (num - 5) / 6);
}
 
// Driver code
public static void Main ()
{
    // initialize the number
    // of sides of a polygon
    int n;
    n = 6;
 
    findTriangles(n);
}
}
 
// This code is contributed by anuj_67..




<script>
 
// javascript program to implement
// the above problem
 
// Function to find the number of triangles
 
function findTriangles(n)
{
    var num = n;
 
    // print the number of triangles
    // having two side common
    document.write( num + " ");
 
    // print the number of triangles
    // having no side common
    document.write( num * (num - 4) * (num - 5) / 6);
}
 
// Driver code
// initialize the number
// of sides of a polygon
var n;
n = 6;
 
findTriangles(n);
 
 
// This code is contributed by 29AjayKumar
 
</script>

Output: 
6 2

 

Time Complexity: O(1)

Auxiliary Space: O(1)
 


Article Tags :