-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path0_1_count_of_2.c
executable file
·37 lines (35 loc) · 1.16 KB
/
0_1_count_of_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* count_of_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: exam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/27 09:19:45 by exam #+# #+# */
/* Updated: 2018/11/27 09:23:08 by exam ### ########.fr */
/* */
/* ************************************************************************** */
int count_of_2(int n)
{
int cnt = 0;
if ( n <= 1)
return (0);
for (int i = 2; i <= n; i++)
{
int temp = i;
while (temp)
{
if (temp % 10 == 2)
cnt++;
temp /= 10;
}
}
return cnt;
}
/*
#include <stdio.h>
int main(void)
{
printf("%d\n", count_of_2(4));
return (0);
}*/