Read the description of lucky four problem

lucky-four-problem-solving-by-c-solution-of-lucky-for-using-c
sloving-of-lucky-four-problem-c-problem-solving-lucky-four
lucky-four-in-c-lucky-four-problem-solving-using-c

This lucky four problem is all about finding the digit 4 in any integers. We have to print the occurrence of the digit four in any integer. We will take the integer from the user and find the number of digit 4 inside that integer. After that we will print the number of 4 inside that integers. We also have to print zero if the digit 4 is not present in that integer.

We have taken this problem from CodeChef to discuss about the solution of this problem for learning purpose only. Obviously you can solve this lucky four problem using different method as well as different programming languages.

We will use C programming language to solve this problem. Now, let’s see the solution to this lucky four problem.

Solution of lucky four problem

We will take an integer at the beginning of this problem which will be the number of test case. The the user will give an integer for each test case. What we have to do is, find the number of 4 inside that integers.

Here, in this bellow program we have stored the given integer in the variable a. After that we have used a while loop till a > 0. Inside the while loop we have divided the integer with 10 which will give the reminder of its last digit. Then check that digit that is it 4 or not. We will update the value of number by dividing it with 10.

Consider, a user have given 244 to check the occurrence of digits lucky four. Then this program will divide it with 10 and store the reminder in rem variable.

rem = 244 % 10

= 4

As the reminder is 4 then the value of count variable will be 1.

The the new value of integer will be 244 / 10

= 24 (Integer part only).

Then the new integer will also be divided by 10 again which will give the reminder 4 again.

The value of count will be 2 and value of new integer will be 24 / 10

= 2

Then again 2 will be divided by 10 which will give the reminder 2.

As 2 is not lucky four, so the value of counter will not be increased here.

Then after checking all the digits of the integer we will print the value of count which will be 2 here.

// solution of lucky four problem by C

#include <stdio.h>

int main(){
  int Test, i;
  scanf("%d", &Test);

  for(i = 1; i <= Test; i++){
    int  a, rem, count = 0;
    scanf("%d", &a);

    while(a > 0){
        rem = a % 10;
        if(rem == 4){
            count++;
        }
        a = a / 10;
    }

    printf("%d\n", count);
    count = 0;
  }
  return 0;
}

Output of lucky four program

Now, compile and run the above lucky four program to get the output like we have given bellow.

5
447474
4
228
0
6664
1
40
1
81
0

In this above output of lucky four program, the first line contains integer 5 which is the testcase for this program. Then the second line is the first integer which we have to check for lucky four. As here the digit 4 remain 4 times then the program have printed 4. Similarly the program have checked all the other input and printed the occurrence of 4 in the output.

In this above program we have used for loop for taking the value of all the integers. We have used while loop also to check the digits and update the number. However you can use any other loop for writing this program. You should also try using different logic to solve this problem.

Previous pageValid triangle

Next pageJump to C projects section