CBSE Class 12 Computer Science Question 21 of 42

Solved 2024 Sample Question Paper CBSE Class 12 Computer Science (083) — Question 3

Back to all questions
3
Question

Question 20

The code given below accepts a number as an argument and returns the reverse number. Observe the following code carefully and rewrite it after removing all syntax and logical errors. Underline all the corrections made.

define revNumber(num):
    rev = 0
    rem = 0
    While num > 0:
        rem == num % 10
        rev = rev * 10 + rem
        num = num//10
        return rev
print(revNumber(1234))
Answer
def revNumber(num):  #correction 1
    rev = 0
    rem = 0
    while num > 0: #correction 2
        rem = num % 10  #correction 3
        rev = rev * 10 + rem
        num = num // 10
    return rev  #correction 4
print(revNumber(1234))
Explanation
  1. correction 1 — The correct keyword to define a function is "def" not "define".
  2. correction 2 — Instead of "While" (capital W) in the while loop, it should be "while" (lowercase w).
  3. correction 3 — The comparison operator "==" in the line rem == num % 10 should be replaced with the assignment operator "=".
  4. correction 4 — Moved the "return" statement outside the while loop to return the reversed number after the loop completes.