Lucky Four
Practice this coding question on Coding Gurukul.
Problem statement
A ticket designer runs a promotion around the digit 4. For each ticket number, the printing system needs to know how many positions contain that digit so it can add the same number of decorative marks. All occurrences matter, including repeated 4s next to each other. The program receives several ticket numbers and produces one count for each ticket. Your task For each nonnegative decimal integer n, count the occurrences of digit 4 in its ordinary decimal representation. Print that count. Other digits do not contribute, and the count is independent of the numeric size of the ticket. Important details The number zero contains no 4. A ticket with no matching digit must produce zero rather than an empty line. Do not count the number of distinct digits or whether at least one 4 exists. Worked example Ticket 40444 has four occurrences of digit 4 and one zero, so its output is 4.
Input format
First line: T. Next T lines: n.
Output format
Print the number of digit-4 occurrences for each test case.
Constraints
1 <= T <= 1000; 0 <= n <= 1000000000000000000.
Sample input
1 40444
Sample output
4
Sign in to run or submit a solution in C++, Java or Python.