Given n points in a plane and no more than two points are collinear, the task is to count the number of triangles in a given plane.

**Examples:**

Input : n = 3 Output : 1 Input : n = 4 Output : 4

Let there are n points in a plane and no three or more points are collinear then number of triangles in the given plane is given by

## C++

`// C++ program to find the number of ` `// triangles in a plane if no more ` `// then two points are collinear. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find number of triangles ` `// in a plane. ` `int` `countNumberOfTriangles(` `int` `n) ` `{ ` ` ` ` ` `// Formula to find number of triangles ` ` ` `// nC3 = n * (n - 1) * (n - 2) / 6 ` ` ` `return` `n * (n - 1) * (n - 2) / 6; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `cout << countNumberOfTriangles(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the number of ` `// triangles in a plane if no more ` `// then two points are collinear. ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find number of triangles ` ` ` `// in a plane. ` ` ` `static` `int` `countNumberOfTriangles(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Formula to find number of triangle ` ` ` `// nC3 = n * (n - 1) * (n - 2) / 6 ` ` ` `return` `n * (n - ` `1` `) * (n - ` `2` `) / ` `6` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` ` ` `System.out.println( ` ` ` `countNumberOfTriangles(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find ` `# the number of triangles ` `# in a plane if no more ` `# then two points are collinear. ` ` ` `# Function to find number ` `# of triangles in a plane. ` `def` `countNumberOfTriangles(n) : ` ` ` ` ` `# Formula to find ` ` ` `# number of triangles ` ` ` `# nC3 = n * (n - 1) * ` ` ` `# (n - 2) / 6 ` ` ` `return` `(n ` `*` `(n ` `-` `1` `) ` `*` ` ` `(n ` `-` `2` `) ` `/` `/` `6` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `4` ` ` `print` `(countNumberOfTriangles(n)) ` ` ` ` ` `# This code is contributed ` `# by ajit ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the ` `// number of triangles in ` `// a plane if no more then ` `// two points are collinear. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find number ` ` ` `// of triangles in a plane. ` ` ` `static` `int` `countNumberOfTriangles(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Formula to find number ` ` ` `// of triangle ` ` ` `// nC3 = n * (n - 1) * ` ` ` `// (n - 2) / 6 ` ` ` `return` `n * (n - 1) * ` ` ` `(n - 2) / 6; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4; ` ` ` ` ` `Console.WriteLine( ` ` ` `countNumberOfTriangles(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the ` `// number of triangles in a ` `// plane if no more then ` `// two points are collinear. ` ` ` `// Function to find number ` `// of triangles in a plane. ` `function` `countNumberOfTriangles(` `$n` `) ` `{ ` ` ` `// Formula to find number ` ` ` `// of triangles nC3 = n * ` ` ` `// (n - 1) * (n - 2) / 6 ` ` ` `return` `$n` `* (` `$n` `- 1) * ` ` ` `(` `$n` `- 2) / 6; ` `} ` ` ` `// Driver code ` `$n` `= 4; ` `echo` `countNumberOfTriangles(` `$n` `); ` ` ` `// This code is contributed ` `// by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Program to check if three points are collinear
- Count of different straight lines with total n points with m collinear
- Number of Triangles that can be formed given a set of lines in Euclidean Plane
- Maximum distance between two points in coordinate plane using Rotating Caliper's Method
- Find the equation of plane which passes through two points and parallel to a given axis
- Number of triangles formed from a set of points on three lines
- Number of triangles that can be formed with given N points
- Python - Find the maximum number of triangles with given points on three lines
- Program to find equation of a plane passing through 3 points
- Program to check whether 4 points in a 3-D plane are Coplanar
- Hammered distance between N points in a 2-D plane
- Number of Integral Points between Two Points
- Prime points (Points that split a number into two primes)
- Forming triangles using points on a square
- Minimum number of points to be removed to get remaining points on one side of axis
- Count number of rotated strings which have more number of vowels in the first half than second half
- GCD of more than two (or array) numbers
- Finding LCM of more than two (or array) numbers without using GCD
- C++ Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers

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.