ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains
The procedure below repeatedly replaces x by the sum of its decimal digits, starting from x = 2^15, until x is a single digit, and prints x. What is printed?
The procedure below repeatedly replaces x by the sum of its decimal digits, starting from x = 2^15, until x is a single digit, and prints x. What is printed?
x = 2^15 while x >= 10: x = sum of the digits of x Print x
- A. 8
- B. 26
- C. 3
- D. 5
Correct answer: A. 8
2^15 = 32768 has digit sum 26, and 26 has digit sum 8. Taking one digit sum gives 26, repeatedly dropping the last digit gives 3, and counting the digits gives 5.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- An array holds the values [3, 0, 2, 0, 1] (positions 0 to 4). The procedure below computes the water trapped above each position as…
- The Fibonacci sequence 1, 1, 2, 3, 5, ... is generated by the loop below, and the procedure prints how many terms are less than or equal…
- The string "aabcccdddd" is compressed by the procedure below, which replaces each run of equal letters by the letter followed by the run…
- The procedure below scans the word "leetcode" from the left and prints the index of the first letter that occurs exactly once in the word…
- The procedure below counts the integers n with 1 <= n <= 60 that are not divisible by 2, 3 or 5 (coprime to 30). What is printed? count =…
- The procedure below counts the pairs i < j in the array [3, 1, 4, 1, 5] whose sum is even. What is printed? count = 0 for i = 0 to n - 2…