 # TCS NQT Intelligence Agency Coding Question With Solution

TCS NQT Intelligence Agency Coding Question With Solution

We at gradjobopenings.com provide free job alerts of freshers job drives. In this website we list on campus job openings for freshers and off campus job openings for freshers and also work from home job openings. This is the best website to apply for off campus drive in India. Visit our website for government job alerts and private job alerts. We also list free interview notes and study materials, one of the best interview study website.comfortable to face the interviews:

Problem statement

An intelligence agency has received reports about some threats. The reports consist of numbers in a mysterious method. 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 performed ‘R’ number of times. The resultant is also 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 action ‘R’ number of times. If the value of ‘R’ is 0, print the output as ‘0’ (without the quotes).

Input format  :

– First line: N (positive integer number).

– Second line: R (positive integer number).

Output format  :

– A positive integer number or print the message (if any) given in the problem statement. (Check the output in Example 1, Example 2).

– Example 1:

– Input :

– 99 -> Value of N

– 3  -> Value of R

– Output :

– 9

– Explanation:

– Here, the number N = 99. Sum of the digits N: 9+9 = 18. Repeat step 2 ‘R’ times i.e. 3 tims  (9+9)+(9+9)+(9+9) = 18 + 18 + 18 =54. Add digits of 54 as we need a single digit 5+4. Hence, the output is 9.

– Example 2:

– Input:

– 1234   -> Value of N

– 2      -> Value of R

– Output:

– 2

– Explanation:

– Here, the number N = 1234. Sum of the digits of N: 1+2+3+4 = 10. Repeat step 2 ‘R’ times i.e. 2 times  (1+2+3+4)+(1+2+3+4)= 10+10=20. Add digits of 20 as we need a single digit. 2+0=2. Hence, the output is 2.

– Constraints:

– 0 < N <= 1000

– 0 <= R <= 50

Program in C

``````#include <stdio.h>

unsigned int count_sum_of_digits(unsigned short int N) {
unsigned int sum = 0;
while (N) {
sum += N % 10;
N /= 10;
}
return sum;
}

int main() {
unsigned int N;
unsigned short int R;
scanf("%u%hu", &N, &R);
if (R == 0) {
printf("0");
return 0;
}
unsigned int sum = count_sum_of_digits(N);
sum *= R;
printf("%u", count_sum_of_digits(sum));
return 0;
}
``````

Program in C++

``````#include <iostream>

using std::cin;
using std::cout;

unsigned int count_sum_of_digits(unsigned short int N) {
unsigned int sum = 0;
while (N) {
sum += N % 10;
N /= 10;
}
return sum;
}

int main() {
unsigned int N;
unsigned short int R;
cin >> N >> R;
if (R == 0) {
cout << "0";
return 0;
}
unsigned int sum = count_sum_of_digits(N);
sum *= R;
cout << count_sum_of_digits(sum);
return 0;
}

``````

Program in Java

``````import java.util.*;
class Main
{
static int count_sum_of_digits (int N) {
int sum = 0;
while (N != 0) {
sum += N % 10;
N /= 10;
}
return sum;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int R = in.nextInt();
in.close();
if (R == 0) {
System.out.print(0);
System.exit(0);
}
int sum = count_sum_of_digits(N);
sum *= R;
System.out.print(count_sum_of_digits(sum));
}
}
``````

Program in Python

``````def count_sum_of_digits(N):
sum_N = 0
while N != 0:
sum_N += N % 10
N //= 10
return sum_N

if __name__ == '__main__':
n = int(input())
r = int(input())
if r == 0:
print(0, end='')
exit(0)
sum_N = count_sum_of_digits(n)
sum_N *= r
print(count_sum_of_digits(sum_N), end = '')
``````