In this post, we will see the solution of the Python Coding Question which is already asked in the TCS NQT Exam.
Problem Statement: A Mega mart maintains a pricing format for all its products. A value N(integral code) is printed on each product. When the scanner reads the value code on the item, the product of all the digits in the code is the price of the item. The task here is to design the software such that given the code of any item the price should be computed for that.
Example: Input: 5544 -> Value of N Output: 400 -> Price Explanation: From the input above Product of the digits 5,5,4,4 5*5*4*4= 400 Hence, output is 400.
Solution 1:
# taking inputs code = input() # initialize price = 1 # iterating through each character for multiplying for char in code: price *= int(char) print(price)
Output:
5544 400
Time Complexity: O(N)
Space Complexity: O(1)
Solution 2:
# taking inputs code = int(input()) # initialize price = 1 # loop will run until code > 0 while code > 0: # taking remainder rem = code % 10 # multiplying with remainder price *= rem # updated the value code = code // 10 print(price)
Output:
5544 400
Time Complexity: O(N)
Space Complexity: O(1)