Skip to main content

Algorithm and flowchart: distance between points

Short answer

Read coordinates; if any is zero, prompt re‑entry. Then compute D² = (x2 - x1)² + (y2 - y1)² and distance D = √(D²). Print D.

Write an algorithm and flowchart of the distance between two points (x1, y1) and (x2, y2), governed by formula D² = (x2 - x1)² + (y2 - y1)². Where x1, x2, y1, y2 are given by user, but should not be zero.

Since the coordinates must not be zero, the input is checked and read again if any value is zero.

Algorithm

  1. Start
  2. Read x1, y1, x2, y2
  3. If any of x1, y1, x2, y2 is 0, print "Values must not be zero" and go to step 2
  4. Calculate D2 = (x2 - x1)^2 + (y2 - y1)^2
  5. Calculate D = sqrt(D2)
  6. Print D
  7. Stop

Flowchart

         ( Start )
             |
     +------>|
     |       v
     |  / Read x1,y1,x2,y2 /
     |       |
     |  < Any value = 0 ? >--No--+
     |       | Yes               |
     |  / Print "Values must     |
     +--- not be zero" /         |
                                 v
              [ D2 = (x2-x1)^2 + (y2-y1)^2 ]
                                 |
                          [ D = sqrt(D2) ]
                                 |
                            / Print D /
                                 |
                             ( Stop )

Example: (1, 2) and (4, 6) give D2 = 9 + 16 = 25, so D = 5.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 2 questions →