TCS NQT Supermarket Coding Question With Solution
A supermarket maintains a pricing format for all its products. A value N is printed on each product. When the scanner reads the value N on the item, the product of all the digits in the value N is the price of the item. The task here is to design the software such that given the code of any item N the product (multiplication) of all the digits of value should be computed (price).
Input format
– First line: A non-negative integer N.
Output format
– First line: A non-negative integer P.
Example 1:
– Input: 5244
– Output: 160
– Explanation: Product of the digits 5,2,4,4 is 160. Hence, the output is 160.
Program in C
#include <stdio.h>
int main() {
unsigned long int n, prod = 1;
scanf("%lu", &n);
while (n != 0) {
prod *= n % 10;
n /= 10;
}
printf("%lu", prod);
return 0;
}
Program in C++
#include <iostream>
int main() {
unsigned long int n, prod = 1;
std::cin >> n;
if (n == 0) {
std::cout << n;
return 0;
}
while (n != 0) {
prod *= n % 10;
n /= 10;
}
std::cout << prod;
return 0;
}
Program in Java
import java.util.Scanner;
class Main {
public static void main (String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int prod = 1;
in.close();
if (N == 0) {
System.out.print(N);
System.exit(0);
}
while (N != 0) {
prod *= N % 10;
N /= 10;
}
System.out.print(prod);
}
}
Program in Python
if __name__ == '__main__':
import math
print(math.prod([int(char) for char in input()]))
Pingback: TCS NQT CODING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS