Lucky Four codechef solution in c++
#include <iostream>
using namespace std;
int countLuckyFours(int num) {
int count = 0;
while (num > 0) {
if (num % 10 == 4) {
count++;
}
num /= 10;
}
return count;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int result = countLuckyFours(n);
cout << result << endl;
}
return 0;
}
This C++ code defines a function countLuckyFours
that counts the occurrences of the digit '4' in a given number. Inside the main
function, it reads an integer t
from the standard input, which represents the number of test cases. Then, for each test case, it reads an integer n
, calls the countLuckyFours
function with n
as an argument, and outputs the count of occurrences of the digit '4' in n
to the standard output.