Question 1[2 marks]

For this question, we are looking at an algorithm used to implement the **divisibility rule for 3**which states that:

*A number is divisible by 3 if the sum of all its digits is divisible by 3.*

Let’s look at the following example: is 378 divisible by 3? Here is the Python code for our recursive algorithm:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
def isDivisibleByThree(n): sum = 0 for digit in str(n): sum = sum + int(digit) if sum>=10: return isDivisibleByThree(sum) else: if sum==3 or sum==6 or sum==9: return True else: return False number = int(input("Type a number")) if isDivisibleByThree(number)==True: print("This number is divisible by 3.") else: print("This number is not divisible by 3.") |

Question 2[4 marks]

Trace this algorithm if the user enters the value 8479
Question 3[4 marks]

Rewrite the function *isDivisibleByThree()*using an iterative approach instead of a recursive approach.

#### Solution...

The solution for this challenge is available to full members!Find out how to become a member:

➤ Members Area