Lucky Four Codechef Solution

Hello Programmers In this post, you will know how to solve the Lucky Four Codechef Solution.

Lucky Four Codechef Solution
Lucky Four Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.


Kostya likes the number 4 much. Of course! This number has such a lot of properties, like:

  • Four is the smallest composite number;
  • It is also the smallest Smith number;
  • The smallest non-cyclic group has four elements;
  • Four is the maximal degree of the equation that can be solved in radicals;
  • There is four-color theorem that states that any map can be colored in no more than four colors in such a way that no two adjacent regions are colored in the same color;
  • Lagrange’s four-square theorem states that every positive integer can be written as the sum of at most four square numbers;
  • Four is the maximum number of dimensions of a real division algebra;
  • In bases 6 and 12, 4 is a 1-automorphic number;
  • And there are a lot more cool stuff about this number!

Impressed by the power of this number, Kostya has begun to look for occurrences of four anywhere. He has a list of T integers, for each of them he wants to calculate the number of occurrences of the digit 4 in the decimal representation. He is too busy now, so please help him.


The first line of input consists of a single integer T, denoting the number of integers in Kostya’s list.

Then, there are T lines, each of them contain a single integer from the list.


Output T lines. Each of these lines should contain the number of occurences of the digit 4 in the respective integer from Kostya’s list.


  • 1 ≤ T ≤ 10^5
  • (Subtask 1): 0 ≤ Numbers from the list ≤ 9 – 33 points.
  • (Subtask 2): 0 ≤ Numbers from the list ≤ 109 – 67 points.






Lucky Four CodeChef Solution in Python

T = int(input())
for _ in range(T):
  n = int(input())
  count = 0
  while n > 0:
    if n % 10 == 4:
          count += 1
    n = n // 10

Lucky Four CodeChef Solutions in CPP

#include <iostream>
using namespace std;
int main() {
    // * Initializing variables
    int numberOfTestCases, number, count;
    // * Accepting the number of test cases
    // * Executing each test case one by one
    while(numberOfTestCases--) {
        // * Accepting number
        // * Initializing the count to 0 for current test case
        count = 0;
        // * Looping while number is not equal to zero
        while(number != 0) {
            // * Incrementing count if the last digit of number is 4
            if(number%10 == 4) {
            // * Removing the last digit from number
            number = number/10;
        // * Displaying the value of count for current test case

Disclaimer: The above Problem (Lucky Four) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: First and Last Digit Codechef Solution

Leave a Reply

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