log in
About and Contact
© 2020 DOUBTCOOL
Hi guys I attempted this question using a simple basic logic i.e., "'int' object is not iterable " .... So
1) I considered my input as a string and i made an iteration to store the '4' separately to another list
called store....
2)Then I just printed the len(store) because........yes you guessed it!!! [Store] contains only 4 ......
If you have any doubts just post it on doubtcool and be cool.....
//this is the answer for the question in c++, @alpha__gamer
#include <iostream>
using namespace std;
int check4(int a)
{
int num,q,r,value=0;
num =a;
while(num!=0)
{
r=num%10;
q=num/10;
num =q;
if(r==4)
{
value++;
}
}
return(value);
}
int main()
{
int i,t,n,occurence;
cin>>t;
for(i=1;i<=t;i++)
{
cin>>n;
occurence = check4(n);
cout<<occurence;
}
}
//this is the answer for the question in c++, @alpha__gamer
#include <iostream>
using namespace std;
int check4(int a)
{
int num,q,r,value=0;
num =a;
while(num!=0)
{
r=num%10;
q=num/10;
num =q;
if(r==4)
{
value++;
}
}
return(value);
}
int main()
{
int i,t,n,occurence;
cin>>t;
for(i=1;i<=t;i++)
{
cin>>n;
occurence = check4(n);
cout<<occurence;
}
}
C++ code
//this is the answer for the question in c++, @alpha__gamer
#include <iostream>
using namespace std;
int check4(int a)
{
int num,q,r,value=0;
num =a;
while(num!=0)
{
r=num%10;
q=num/10;
num =q;
if(r==4)
{
value++;
}
}
return(value);
}
int main()
{
int i,t,n,occurence;
cin>>t;
for(i=1;i<=t;i++)
{
cin>>n;
occurence = check4(n);
cout<<occurence;
}
}
praveen_
#include<iostream>
#include<string>
using namespace std;
int main(){
int kList[100001]={0};
int n=0,x=0,count=0,total;
int occ = 0;
cin>>n;
total=n;
while(n>0){
cin>>x;
kList[count]=x;
count++;
n--;
}
for(int i = 0;i<total;i++){
string s = to_string(kList[i]);
for(int j = 0;j<s.length();j++){
if(((int)s[j]-48)==4){
occ++;
}
}
cout<<occ<<endl;
occ=0;
s.clear();
}
return 0;
}
Read problems statements in Mandarin Chinese and Russian.
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.
Input
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
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.
Constraints
1 ≤ T ≤ 105
(Subtask 1): 0 ≤ Numbers from the list ≤ 9 - 33 points.
(Subtask 2): 0 ≤ Numbers from the list ≤ 109 - 67 points.
Sample Input 1
5
447474
228
6664
40
81
Sample Output 1
4
0
1
1
0
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n,count=0,x;
cin>>n;
while(n>0){
x = n%10;
n = n/10;
if(x==4)
count++;
}
cout<<count<<endl;
}
return 0;
}