TCS NQT Coding Question-9: Single Digit Sum

TCS NQT Coding Question-9: Single Digit Sum

In this post, we will see the solution of the Python Coding Question which is earlier asked in the TCS NQT Exam.

Problem Statement: An intelligence team has received reports about some threats. The reports consist of numbers in a mysterious way. There is a number “n” and another number “r”. Those numbers are studied thoroughly, and it is concluded that all digits of the number ‘n’ are summed up and this action is needed to be performed ‘r’ number of times. The resultant should also be a single digit that is yet to be deciphered. The task here is to find the single digit sum of the given number ‘n’ by repeating the same action ‘r’ number of times.
If the value of ‘r’ is 0, print the output as ‘0’.

Example 1:
Input:
Value of n -> 89
Value of r -> 3
Output:
Possible ways to fill the cistern -> 6
Explanation:
Here, the number n=89
Sum of the digits n: 8+9 = 17
Repeat step 2 'r' times i.e. 3 times (8+9) +(8+9) +(8+9) = 17+17+17 = 51
Add digits of 51 as we need a single digit 5 + 1
Hence, the output is 6.
Example 2:
Input:
Value of n -> 1235
Value of r -> 2
Output:
Possible ways to fill the cistern -> 4
Explanation:
Here, the number n=1235
Sum of the digits of n: 1+2+3+5 =11
Repeat step 2 'r' times i.e. 2 times (1+2+3+5) +(1+2+3+5) = 11+11=22
Add digits of 22 as we need a single digit. 2+2=4
Hence, the output is 4.

Constraints:
0<n<=2000
0<=r<=50
The Input format for testing is given below:
The candidate has to write the code to accept 2 inputs:
First input- Accept value for n (positive integral number)
Second input: Accept value for r (Positive integral number)
The output format for testing:
The output should be a positive integral number or print the message (if any) given in the problem statement. (Check the output in Example 1, Example 2).

Solution:

# taking inputs
n = input()
r = int(input())

# initialising the variable  
sum_rslt= 0

# sum of digits
for digit in n:
        sum_rslt += int(digit)

# check for value of r
if r == 0 :
        print(0)

else :
        # multiplying sum of digits value with r times
        rslt = str(sum_rslt * r)

        # perform the same operation untill
        # not getting the single digit sum
        while len(rslt) > 1 :
            sum_rslt_2 = 0

            # sum of digits
            for digit in rslt :
                sum_rslt_2 += int(digit)

            rslt = str(sum_rslt_2)

        # print the final single digit value
        print(rslt)

Output:

89
3
6

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