# C++ program to find the slope of a line

**Coltrane is a master saxophonist by night, and the Lead C++ Developer at Google by day. He is teaching an "Intro to C++" class at Google's Chicago campus this week. He asks his students to solve this problem:**

**"Write a well-commented C++ program that will do the following:**

**1.) Prompt the user to enter the coordinates for two points on a line: x1, y1, x2, and y2 (for example, the user would enter 1, 4, 10, 20).**

**2.) Based on the coordinates entered, the computer program should output the slope of the file. For example, the computer program would respond "the slope of the line that you entered is 7/12"."**

Certainly! Here's a well-commented C++ program that accomplishes the task of calculating and displaying the slope of a line given two sets of coordinates:

#include <iostream>

int main() {

// Declare variables to store coordinates

double x1, y1, x2, y2;

// Prompt the user to enter the coordinates

std::cout << "Enter the coordinates of the first point (x1, y1): ";

std::cin >> x1 >> y1;

std::cout << "Enter the coordinates of the second point (x2, y2): ";

std::cin >> x2 >> y2;

// Calculate the slope

double slope = (y2 - y1) / (x2 - x1);

// Check for division by zero (undefined slope)

if (x1 == x2) {

std::cout << "The line is vertical, and the slope is undefined." << std::endl;

} else {

// Output the result

std::cout << "The slope of the line that you entered is " << slope << std::endl;

}

return 0;

}

This program first prompts the user to enter the coordinates of two points, (x1, y1) and (x2, y2). It then calculates the slope using the formula `(y2 - y1) / (x2 - x1)`

. If the denominator (x2 - x1) is zero, it checks for division by zero and informs the user that the slope is undefined in the case of a vertical line. Otherwise, it displays the calculated slope.