Table of Contents

## Python Exercise 12 Problem, Part 1

In this Python exercise, write a Python program that’s given the range of numbers between 4000 and 9000 to validate that each digit in the range is divisible by 4.

For example, if the number 1000 was given, each digit would need to be divisible by 2 and the number 1000 is not. Meaning the digit 1 is not divisible by 2 and so on.

**Solution ****Python Exercise 12, Part 1**

**Python Code Input:**

div_by_4 = [] for d in range(4000, 9000): x = str(d) if (int(x[0])%4==0) and (int(x[1])%4==0) and (int(x[2])%4==0) and (int(x[3])%4==0): div_by_4.append(x) print("Each digit of the numbers within range are divisible by 4 : ") print(",".join(div_by_4))

**Python Code Output:**

Each digit of the numbers within range are divisible by 4 : 4000,4004,4008,4040,4044,4048,4080,4084,4088,4400,4404,4408,4440,4444,4448,4480,4484,4488,4800,4804,4808,4840,4844,4848,4880,4884,4888,8000,8004,8008,8040,8044,8048,8080,8084,8088,8400,8404,8408,8440,8444,8448,8480,8484,8488,8800,8804,8808,8840,8844,8848,8880,8884,8888

## Python Exercise 12 Problem, Part 2

In Python exercise, part 2, using the same Python program with the given range between 4000 and 9000, validate that each digit in the range is divisible by 4 and 8. This should be very simple with an extra line of code that’s already used.

**Solution ****Python Exercise 12, Part 2**

**Input:**

div_by_4_8= [] for d in range(4000, 9000): x = str(d) if (int(x[0])%4==0) and (int(x[1])%4==0) and (int(x[2])%4==0) and (int(x[3])%8==0): if (int(x[0])%8==0) and (int(x[1])%8==0) and (int(x[2])%8==0) and (int(x[3])%8==0): div_by_4_8.append(x) print("Each digit of the numbers within range are divisible by 4 and 8 : ") print(",".join(div_by_4_8))

**Output:**

Each digit of the numbers within range are divisible by 4 and 8 : 8000,8008,8080,8088,8800,8808,8880,8888