TCS NQT Coding Question-6: Calculate Price Of The Item

TCS NQT Coding Question-6: Calculate Price Of The Item

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)

Leave a Reply

Your email address will not be published. Required fields are marked *

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds