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

Check Geometric Progression

Check equal ratios between three values. Students trace division guards, nested decisions, and why this simple geometric-progression test rejects zeros.

AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 6 of 10
check-geometric-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 GP (avoid division by zero)
7if a != 0 and b != 0:
8 if (b / a) == (c / b):
9 print("Geometric progression")
10 else:
11 print("Not a geometric progression")
12else:
13 print("Cannot form GP with zero")

Output

Enter first number: 2
Enter second number: 4
Enter third number: 8
Geometric progression

Enter first number: 2
Enter second number: 5
Enter third number: 8
Not a geometric progression

In this one

  • Compare consecutive ratios
  • Guard division by zero
  • Understand implementation limits

How it works

  1. 1

    Read the three floats.

  2. 2

    Require nonzero a and b before division.

  3. 3

    Compare b / a with c / b and print.

Edge cases

What if a is zero?

What if a is zero?

The outer guard prints the zero-related message before division.

Can float ratios have rounding trouble?

Can float ratios have rounding trouble?

Yes. Exact equality may be unsuitable for some decimal inputs.

What's going on

A geometric progression uses a constant multiplier between consecutive terms. This implementation compares b / a with c / b, so both denominators must be nonzero.

The outer guard prevents division by zero; the inner comparison checks the ratios. Input 2, 4, 8 passes with ratio two. The rejection message is a limitation of this division-based version, not a universal mathematical rule that zero can never occur in a progression.

What if a is zero? The outer guard prints the zero-related message before division.

Can float ratios have rounding trouble? Yes. Exact equality may be unsuitable for some decimal inputs.

Quiz

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

Question 1

Which ratios are compared?

Pick an answer to check it.

Question 2

What if a is zero?

Pick an answer to check it.

Questions people ask

What if a is zero?

The outer guard prints the zero-related message before division.

Can float ratios have rounding trouble?

Yes. Exact equality may be unsuitable for some decimal inputs.

Which ratios are compared?

b / a and c / b.