---
title: "Algorithm and flowchart: distance between points"
subject: "Computer Programming"
chapter: 2
key: "semester_1/computer_programming:algo-flowchart-distance"
source: https://www.bctengineer.com/q/semester_1/computer_programming/algo-flowchart-distance
site: BCT Engineering
---

# Algorithm and flowchart: distance between points

Computer Programming · Chapter 2: Problem solving using Computer

- Asked in: 2067 Asar, 5+5 marks
- Video: [C++  Tutorial | Flow Charts to Code | Distance in two points](https://www.youtube.com/watch?v=9quUSPebEck) — Fahmi Raymond
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/algo-flowchart-distance
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-2

## Question

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.

## Short answer

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

## Answer

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
```text
         ( 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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. If any coordinate is zero, the algorithm
   A. continues to calculate
   B. stops permanently
   C. prints a message and reads the values again
   D. sets the value to 1
   Answer: C. Validation loop. (Key point: Input check)

2. D is obtained from
   A. D = (x2-x1)+(y2-y1)
   B. D = x2*y2
   C. D = D2^2
   D. D = sqrt((x2-x1)^2 + (y2-y1)^2)
   Answer: D. D2 is the squared distance. (Key point: Formula)

3. Distance between (1,2) and (4,6) is
   A. 5
   B. 25
   C. 7
   D. 3
   Answer: A. 9+16=25, sqrt=5. (Key point: Example)

## Related in Computer Programming

- [Algorithm for numbers divisible by 9 not 6](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgo-flowchart-divisible-9-not-6) — Computer Programming, chapter 2
- [Algorithm and flowchart help; Armstrong flowchart](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgorithm-flowchart-armstrong) — Computer Programming, chapter 2
- [Algorithm and flowchart for largest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgorithm-flowchart-largest-n) — Computer Programming, chapter 2
- [Algorithm and flowchart: quadratic roots](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgo-flowchart-quadratic) — Computer Programming, chapter 2

## Related in other subjects

- [Two-mass system X1/F, X2/F with F-V analogy](https://www.bctengineer.com/question.md?key=semester_3%2Fcontrol_systems%3Afv-tf-m1-m2-k1b1-k2b2) — Control Systems, chapter 2, asked 3×
- [Free body diagram and X1/F, X2/F of two-mass system](https://www.bctengineer.com/question.md?key=semester_3%2Fcontrol_systems%3Afbd-m1-m2-k1k2-b1b2) — Control Systems, chapter 2
- [Practical factors in sampling; sampling interval for X1(t)X2(t)](https://www.bctengineer.com/question.md?key=semester_6%2Fcommunication_systems%3Asampling-practical-x1x2) — Communication Systems, chapter 4
- [Find x3[n] from X3=X1X2, x1 5-point, x2 4-point](https://www.bctengineer.com/question.md?key=semester_7%2Fdigital_signal_analysis_and_processing%3Adft-product-x3-73s) — Digital Signal Analysis and Processing, chapter 7, asked 3×
