Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Check Arithmetic Progression

Check whether three entered values form an arithmetic progression. A lesson on equal consecutive differences, input order, and floating-point comparisons.

AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 5 of 10
check-arithmetic-progression.py
1# Take three numbers
2a = float(input("Enter first number: "))
3b = float(input("Enter second number: "))
4c = float(input("Enter third number: "))
5
6# Check AP
7if (b - a) == (c - b):
8 print("Arithmetic progression")
9else:
10 print("Not an arithmetic progression")

Output

Enter first number: 2
Enter second number: 4
Enter third number: 6
Arithmetic progression

Enter first number: 2
Enter second number: 5
Enter third number: 6
Not an arithmetic progression

In this one

  • Compare consecutive differences
  • Allow decreasing progressions
  • Recognize float equality limits

How it works

  1. 1

    Read a, b, and c.

  2. 2

    Calculate b - a and c - b.

  3. 3

    Print whether those differences are equal.

Edge cases

Does 6, 4, 2 pass?

Does 6, 4, 2 pass?

Yes. Both differences are minus two.

Do three equal numbers pass?

Do three equal numbers pass?

Yes. Both differences are zero.

What's going on

An arithmetic progression keeps the same difference from one term to the next. The program compares b - a with c - b in the entered order.

Input 2, 4, 6 passes because both differences are two. A decreasing sequence also works when both differences are equal and negative. The code compares floats exactly, so decimal rounding can affect some mathematically equal differences.

Does 6, 4, 2 pass? Yes. Both differences are minus two.

Do three equal numbers pass? Yes. Both differences are zero.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which property defines this check?

Pick an answer to check it.

Question 2

Does 6, 4, 2 pass?

Pick an answer to check it.

Questions people ask

Does 6, 4, 2 pass?

Yes. Both differences are minus two.

Do three equal numbers pass?

Yes. Both differences are zero.

Which property defines this check?

Equal consecutive differences.