- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find slope of a line in C++

In this problem, we are given the coordinates of two points of a line. Our task is to create a *Program to find slope of a line in C++*.

**Problem Description** − We will find the slope of the line using the coordinates of two points on the line that are given.

**Let’s take an example to understand the problem**

## Input

p1(-1, 1), p2(3, 3)

## Output

½ = 0.5

## Solution approach

To find the slope of the line, we will use the geometrical formula defined to find the slope of a line using any two points P1(x1, y1) and P2(X2, Y2) that lie on the line.

Slope = (Y2 - Y1)/(X2 - X1)

**Program to illustrate the working of our solution**

## Example

#include<iostream> using namespace std; float calcSlope(float point[2][2]){ float slope = ( (point[1][1]-point[0][1]) / (point[1][0] - point[0][0])); return slope; } int main() { float points[2][2] = {{-1, 1}, {3, 3}}; cout<<"The slope of the line is "<<calcSlope(points); }

## Output

The slope of the line is 0.5

- Related Articles
- How to find the 95% confidence interval for the slope of regression line in R?
- Program to find the mid-point of a line in C++
- Find the slope of the given number using C++
- How is the slope of a capital market line (Sharpe Ratio) defined?
- Program to find line passing through 2 Points in C++
- How to add a regression line to a plot in base R if intercept and slope are given?
- Swift Program to Find the Mid-point of a Line
- Program to find the number of possible position in a line in Python
- How to create a scatterplot with regression line using ggplot2 with 0 intercept and slope equals to 1 in R?
- C++ program to find the number of triangles amongst horizontal and vertical line segments
- C++ Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
- What is a slope in motion?
- C program to remove a line from the file
- Find foot of perpendicular from a point in 2D plane to a Line in C++
- Program to find Circumcenter of a Triangle in C++

Advertisements