# Number of triangles formed from a set of points on three lines

Given three integers *m, n and k* that store the number of points on lines l1, l2 and l3 respectively that do not intersect. The task is to find the number of triangles that can possibly be formed from these set of points.

**Examples:**

Input:m = 3, n = 4, k = 5Output:205Input:m = 2, n = 2, k = 1Output:10

**Approach:**

- The total number of points are (m + n + k) which must give number of triangles.
- But ‘m’ points on ‘l1’ gives combinations which cannot form a triangle.
- Similarly, and number of triangles can not be formed.
- Therefore, Required Number of Triangles =

Below is the implementation of the above approach:

## C++

`// CPP program to find the possible number ` `// of triangles that can be formed from ` `// set of points on three lines ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns factorial of a number ` `int` `factorial(` `int` `n) ` `{ ` ` ` `int` `fact = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `fact = fact * i; ` ` ` `return` `fact; ` `} ` ` ` `// calculate c(n, r) ` `int` `ncr(` `int` `n, ` `int` `r) ` `{ ` ` ` ` ` `return` `factorial(n) ` ` ` `/ (factorial(r) * factorial(n - r)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `m = 3, n = 4, k = 5; ` ` ` `int` `totalTriangles ` ` ` `= ncr(m + n + k, 3) ` ` ` `- ncr(m, 3) - ncr(n, 3) - ncr(k, 3); ` ` ` `cout << totalTriangles << endl; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java program to find the possible number ` `// of triangles that can be formed from ` `// set of points on three lines ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns factorial of a number ` `static` `int` `factorial(` `int` `n) ` `{ ` ` ` `int` `fact = ` `1` `; ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `fact = fact * i; ` ` ` `return` `fact; ` `} ` ` ` `// calculate c(n, r) ` `static` `int` `ncr(` `int` `n, ` `int` `r) ` `{ ` ` ` ` ` `return` `factorial(n) ` ` ` `/ (factorial(r) * factorial(n - r)); ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `m = ` `3` `, n = ` `4` `, k = ` `5` `; ` ` ` `int` `totalTriangles = ncr(m + n + k, ` `3` `) - ` ` ` `ncr(m, ` `3` `) - ncr(n, ` `3` `) - ncr(k, ` `3` `); ` ` ` `System.out.println (totalTriangles); ` ` ` ` ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find the ` `# possible number of triangles ` `# that can be formed from set of ` `# points on three lines ` ` ` ` ` `# Returns factorial of a number ` `def` `factorial(n): ` ` ` `fact ` `=` `1` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `): ` ` ` `fact ` `=` `fact ` `*` `i ` ` ` `return` `fact ` ` ` `# calculate c(n, r) ` `def` `ncr(n, r): ` ` ` ` ` `return` `(factorial(n) ` `/` `/` `(factorial(r) ` `*` ` ` `factorial(n ` `-` `r))) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `m ` `=` `3` ` ` `n ` `=` `4` ` ` `k ` `=` `5` ` ` `totalTriangles ` `=` `(ncr(m ` `+` `n ` `+` `k, ` `3` `) ` `-` ` ` `ncr(m, ` `3` `) ` `-` `ncr(n, ` `3` `) ` `-` ` ` `ncr(k, ` `3` `)) ` ` ` `print` `(totalTriangles) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the possible number ` `// of triangles that can be formed from ` `// set of points on three lines ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns factorial of a number ` `static` `int` `factorial(` `int` `n) ` `{ ` ` ` `int` `fact = 1; ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `fact = fact * i; ` ` ` `return` `fact; ` `} ` ` ` `// calculate c(n, r) ` `static` `int` `ncr(` `int` `n, ` `int` `r) ` `{ ` ` ` ` ` `return` `factorial(n) / (factorial(r) * ` ` ` `factorial(n - r)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `m = 3, n = 4, k = 5; ` ` ` ` ` `int` `totalTriangles = ncr(m + n + k, 3) - ` ` ` `ncr(m, 3) - ncr(n, 3) - ` ` ` `ncr(k, 3); ` ` ` ` ` `Console.WriteLine (totalTriangles); ` `} ` `} ` ` ` `// This code is contributed ` `// by anuj_67.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the possible ` `// number of triangles that can be ` `// formed from set of points on ` `// three lines ` ` ` `// Returns factorial of a number ` `function` `factorial(` `$n` `) ` `{ ` ` ` `$fact` `= 1; ` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `$fact` `= ` `$fact` `* ` `$i` `; ` ` ` `return` `$fact` `; ` `} ` ` ` `// calculate c(n, r) ` `function` `ncr(` `$n` `, ` `$r` `) ` `{ ` ` ` `return` `factorial(` `$n` `) / (factorial(` `$r` `) * ` ` ` `factorial(` `$n` `- ` `$r` `)); ` `} ` ` ` `// Driver code ` `$m` `= 3; ` `$n` `= 4; ` `$k` `= 5; ` `$totalTriangles` `= ncr(` `$m` `+ ` `$n` `+ ` `$k` `, 3) - ` ` ` `ncr(` `$m` `, 3) - ncr(` `$n` `, 3) - ` ` ` `ncr(` `$k` `, 3); ` `echo` `$totalTriangles` `. ` `"\n"` `; ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

**Output:**

205

## Recommended Posts:

- Number of Triangles that can be formed given a set of lines in Euclidean Plane
- Number of triangles that can be formed with given N points
- Number of triangles in a plane if no more than two points are collinear
- Maximum points of intersection n lines
- Find whether only two parallel lines contain all coordinates points or not
- Count of different straight lines with total n points with m collinear
- Count of triangles with total n points with m collinear
- Prime points (Points that split a number into two primes)
- Number of Integral Points between Two Points
- Count the number of possible triangles
- Number of triangles after N moves
- Number of Triangles in an Undirected Graph
- Count number of right triangles possible with a given perimeter
- Count number of unique Triangles using STL | Set 1 (Using set)
- Number of possible Triangles in a Cartesian coordinate system

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.