KnowledgeBoat Logo

Computer Applications

The co-ordinates of two points A and B on a straight line are given as (x1,y1) and (x2,y2). Write a program to calculate the slope (m) of the line by using formula:

Slope = (y2 - y1) / (x2 - x1)

Take the co-ordinates (x1,y1) and (x2,y2) as input.

Java

Input in Java

47 Likes

Answer

import java.util.Scanner;

public class KboatLineSlope
{
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter x coordinate of point A: ");
        int x1 = in.nextInt();
        
        System.out.print("Enter y coordinate of point A: ");
        int y1 = in.nextInt();
        
        System.out.print("Enter x coordinate of point B: ");
        int x2 = in.nextInt();
        
        System.out.print("Enter y coordinate of point B: ");
        int y2 = in.nextInt();
        
        float lineSlope = (y2 - y1) / (float)(x2 - x1);
        
        System.out.println("Slope of line: " + lineSlope);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of The co-ordinates of two points A and B on a straight line are given as (x1,y1) and (x2,y2). Write a program to calculate the slope (m) of the line by using formula: Slope = (y2 - y1) / (x2 - x1) Take the co-ordinates (x1,y1) and (x2,y2) as input.

Answered By

23 Likes


Related Questions