CodeChef::LUCKFOUR

 

#include <iostream>
using namespace std ;

int main(){
int t ;
cin >> t ;
while (t--){
int n ;
cin >> n ;
int temp{n};
int count_digits{0} ;
int count_fours{0};
while (n != 0){
n = n/10 ;
count_digits ++ ;
}
long int array[10];
for (int i = count_digits - 1; i >=0 ; --i) {
array[i] = temp % 10 ;
temp = temp/10 ;
}
for (int i = 0; i < count_digits; ++i) {
if (array[i] == 4){
count_fours ++ ;
}

}
cout << count_fours << '\n' ;


}

return 0;
}
                                   ALTER:
 
// CODE: LUCKFOUR

// logic:
//         we input the number then we find the digit
//           in the number which is equal to 4.
//
//           input number is stored in number, digits are
//           stored in digit. counter is stored in count 
//           which is assigned an initial value 0.
//           we take the digit from the number by 
//             digit = number % 10;
//           and the reduce the number to 
//             number = number / 10;
//           if digit == 4 
//             then count increments
//           after checking all the digits
//           count is printed.

#include <iostream>
using namespace std;

int main() {
    int testcase;
    cin >> testcase;
    while (testcase --) {
        int number, count = 0, digit;
        cin >> number;

        while (number != 0) {
            digit = number % 10;
            number = number / 10;
            if (digit == 4) {
                count ++;
            }
        }
        cout << count << endl;
    }
    return 0;
}

Comments

Popular posts from this blog

CodeChef::CSUB

Atcoder Educational Dp contest :: C vacation

How Recursion Works?