Educational Articles

Algorithm to find the slope of a line segment.

Read the important current affairs of 15 July 2026 for SSC, Banking, UPSC, Railway and all competitive exams.

16 Oct 2024 5 Min Read Quizer Team 271 Views

16

October 2024
Write an algorithm to find the slope of a line segment whose end point coordinates are (x1, y1) and (x2, y2). The algorithm gives output whether the slope is positive, negative or zero. Transform your algorithm into C program.

Here’s a simple algorithm to find the slope of a line segment given its endpoints (x1,y1)(x1,y1) and (x2,y2)(x2,y2). The slope mm is calculated using the formula:

m=(y2−y1)/ (x2−x1)

Algorithm Steps:


 1. Input the coordinates x1x1, y1y1, x2x2, and y2y2.


 2. Check if x1x1 is equal to x2x2:
        If true, the slope is undefined (vertical line).
        If false, calculate the slope using the formula.


 3. Determine the nature of the slope:
        If m>0,  output "Positive slope Positive".
        If m>0,  output "Positive slope Negative".

         If m==0, output "Zero slope".

C Program


Here’s how you can implement the above algorithm in C:

#include 
int main() {
    float x1, y1, x2, y2, slope;
    // Input coordinates
    printf("Enter coordinates (x1, y1, x2, y2): ");
    scanf("%f %f %f %f", &x1, &y1, &x2, &y2);
    // Check for vertical line
    if (x1 == x2) {
        printf("The slope is undefined (vertical line).\n");
    } else {
        // Calculate the slope
        slope = (y2 - y1) / (x2 - x1);
        // Determine the nature of the slope
        if (slope > 0) {
            printf("Positive slope\n");
        } else if (slope < 0>           
             printf("Negative slope\n");
        } else {
            printf("Zero slope\n");
        }
    }
    return 0;
}


Explanation of the C Program:


    The program prompts the user to enter the coordinates of the two endpoints.
    It checks if the line is vertical by comparing x1x1 and x2x2.
    If the line is not vertical, it calculates the slope and checks its value to determine if it's positive, negative, or zero, then outputs the result accordingly.

You can compile and run this code using any standard C compiler.

Why This Content Matters

This content is prepared to help students and competitive-exam aspirants understand important topics, revise key information, and strengthen their exam preparation.

  • Quickly revise important facts and concepts
  • Improve General Knowledge and exam awareness
  • Support preparation for competitive and government exams
  • Build a consistent and effective study routine
Quizer Team
About the Author

Quizer Team

The Quizer Team creates exam-focused educational content, current affairs, general knowledge, study notes, and preparation resources to help students and competitive-exam aspirants learn, practice, and stay updated.

Educational Content Exam Focused Learning Resources

📢 Join Our WhatsApp Channel

Get Daily GK, Current Affairs, Amazing Facts & Quiz Updates.

🚀 Join Now

Join Monthly Challenge

Compete, Learn & Win exciting prizes.

  • Cash Rewards
  • E-Certificates
  • Leaderboard Ranking
  • Daily Practice
Join Now

Related Post

Educational Articles
17 Oct 2024 145
Read More
Educational Articles
17 Oct 2024 201
Read More
Educational Articles
16 Oct 2024 185
Read More