# Java Program to Find Count of Different Straight Lines with Total n Points With m collinear

In the previous article, we have seen Java Program to Check if Line Passes Through the Origin

In this article we will discuss about how to find count of different straight lines we can draw with total n points with m collinear using Java programming language.

## Java Program to Find Count of Different Straight Lines with Total n Points With m collinear

Before jumping into the program directly, let’s first know how can we count the number of lines we can draw with m collinear points out of n points.

Explanation:

Formula to Find Total Number Of Lines That Can Be Drawn: nC2mC2 + 1

Example:

When n=5 and Nm=3

Number of lines : nC2mC2 + 1

=> 5C23C2 + 1

=> 5!/(2!(5-2)!) – 3!/(2!(3-2)!) + 1

=> 120/12 – 6/2 + 1

=> 10-3 + 1

=> 8

Let’s see different ways to find number of lines.

### Method-1: Java Program to Find Count of Different Straight Lines with Total n Points With m collinear By Using Static Input Values

Approach:

• Declare the value for ‘m’ and ‘n’.
• Then call the straightLinesCount() method by passing m and n value as parameter.
• This method then calls the combination() method to find out 5C23C2 + 1 i.e. the number of straight lines.
• Then print the result.

Program:

// JAVA Code to count number of
// straight lines passing through

public class Main
{
public static void main(String[] args)
{
int n =5, m=3;
// Prints the total number of lines
System.out.println("The number of straight lines with total of "+n+" points and "
+m+" collinear points is "+straightLinesCount(m,n));
}

// Returns number of straight lines
public static int straightLinesCount(int m, int n)
{
int lines = 0;
// Calculates the number of lines
lines = combination(n,2) - combination(m,2) + 1;
return lines;
}

// Function that returns the number of combinations
public static int combination(int n, int r)
{
return factorial(n) / (factorial(r) * factorial(n - r));
}

// Function that returns the factorial of the number
public static int factorial(int number) {
int res = 1;
for (int factor = 2; factor <= number; factor++)
{
res *= factor;
}
return res;
}
}

Output:

The number of straight lines with total of 5 points and 3 collinear points is 8

### Method-2: Java Program to Find Count of Different Straight Lines with Total n Points With m collinear By Using User Input Values

Approach:

1. Take user input the value for ‘m’ and ‘n’.
2. Then call the straightLinesCount() method by passing m and n value as parameter.
3. This method then calls the combination() method to find out 5C23C2 + 1 i.e. the number of straight lines.
4. Then print the result.

Program:

// JAVA Code to count number of
// straight lines passing through

import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
// Scanner class to take input from user
Scanner scan = new Scanner(System.in);
System.out.print("Enter total points and number of collinear points : ");
int n = scan.nextInt(), m=scan.nextInt();
// Prints the total number of lines
System.out.println("The number of straight lines with total of "+n+" points and "
+m+" collinear points is "+straightLinesCount(m,n));
}

// Returns number of straight lines
public static int straightLinesCount(int m, int n)
{
int lines = 0;
// Calculates the number of lines
lines = combination(n,2) - combination(m,2) + 1;
return lines;
}

// Function that returns the number of combinations
public static int combination(int n, int r)
{
return factorial(n) / (factorial(r) * factorial(n - r));
}

// Function that returns the factorial of the number
public static int factorial(int number)
{
int res = 1;
for (int factor = 2; factor <= number; factor++)
{
res *= factor;
}
return res;
}
}

Output:

Enter total points and number of collinear points : 9 5
The number of straight lines with total of 9 points and 5 collinear points is 27

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Articles: