AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Easywhile loopdigits

~75s

Reversing a number

/ is integer division and MOD is the remainder. What is printed?

Integer n, r
Set n = 4021
Set r = 0
While n > 0
    Set r = r * 10 + (n MOD 10)
    Set n = n / 10
End While
Print r
Show the answer and the worked solution

Correct answer: A.

1204

Worked solution

  1. First round: r = 0 * 10 + 1 = 1, n = 402.
  2. Second round: r = 1 * 10 + 2 = 12, n = 40.
  3. Third round: r = 12 * 10 + 0 = 120, n = 4.
  4. Fourth round: r = 120 * 10 + 4 = 1204 and n = 0, so the loop ends and 1204 is printed.

Answer: 1204.

The idea

Peel the last digit with MOD 10, drop it with / 10, and build the answer as r * 10 + digit. The interior zero is the detail worth checking, since it contributes nothing visible until a later digit shifts it along.

Shortcut. r = r × 10 + last digit is digit reversal, so write 4021 backwards: 1204, with the interior 0 staying in the middle.

Common trap. 1240 comes from reversing only the non-zero digits (4 2 1 → 1 2 4) and tacking the zero on the end, as if a 0 digit added nothing where it stood.

Reversing a number — Pseudocode Aptitude Question

Reversing a number: / is integer division and MOD is the remainder. What is printed? Answer and worked solution on CodeKairo.

Question

/ is integer division and MOD is the remainder. What is printed?

Integer n, r
Set n = 4021
Set r = 0
While n > 0
    Set r = r * 10 + (n MOD 10)
    Set n = n / 10
End While
Print r

Options

  1. 1204
  2. 1240
  3. 4021
  4. 12040
Show the answer and the worked solution

Correct answer: A. 1204

Worked solution

  1. First round: r = 0 * 10 + 1 = 1, n = 402.
  2. Second round: r = 1 * 10 + 2 = 12, n = 40.
  3. Third round: r = 12 * 10 + 0 = 120, n = 4.
  4. Fourth round: r = 120 * 10 + 4 = 1204 and n = 0, so the loop ends and 1204 is printed.

Answer: 1204.

The idea

Peel the last digit with MOD 10, drop it with / 10, and build the answer as r * 10 + digit. The interior zero is the detail worth checking, since it contributes nothing visible until a later digit shifts it along.

Shortcut. r = r × 10 + last digit is digit reversal, so write 4021 backwards: 1204, with the interior 0 staying in the middle.

Common trap. 1240 comes from reversing only the non-zero digits (4 2 1 → 1 2 4) and tacking the zero on the end, as if a 0 digit added nothing where it stood.

All Pseudocode questions · placement mock tests