Check Geometric Progression

Take three numbers and check if they are in geometric progression.

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

What's going on

In GP, ratio between consecutive terms is constant.

Key Concepts:

Check if (b/a) == (c/b)
Handle division by zero
Common ratio must be same