141k views
5 votes
Use a while loop. Compare the digits; do not write a large if-else for all possible same-digit numbers (11, 22, 33, …, 88), as that approach would be cumbersome for larger ranges.

1 Answer

1 vote

Answer:

The solution code is written in Python 3:

  1. num = 11
  2. while(num <=88):
  3. firstDigit = num // 10
  4. secondDigit = num % 10
  5. if(firstDigit == secondDigit):
  6. print(num)
  7. num += 1

Step-by-step explanation:

Firstly, create a variable, num, to hold the starting value, 11 (Line 1)

Create a while loop and set the loop condition to continue the loop while num smaller or equal to 88 (Line 3).

Within the loop, we can use // to perform the floor division and get the first digit of the num. We use modulo operator % to get the second digit of the num. (Line 4 - 5)

If the firstDigit is equal to secondDigit, print the number (Line 7 -8). Lastly, increment the num by one and proceed to next iteration (Line 10).

User Reynicke
by
5.0k points