Skip to content

Commit 1971d25

Browse files
committed
Harshad number C implementation
1 parent 4c9f3f8 commit 1971d25

1 file changed

Lines changed: 53 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
** Harshad number implementation
3+
*/
4+
5+
#include <stdio.h>
6+
7+
static int sumDigit(int n)
8+
{
9+
int sum = 0;
10+
11+
while (n)
12+
{
13+
sum += n % 10;
14+
n /= 10;
15+
}
16+
return (sum);
17+
}
18+
19+
/*
20+
** Return if the number is Harshad or not
21+
*/
22+
int isHarshad(int n)
23+
{
24+
int sum = sumDigit(n);
25+
26+
if (!sum)
27+
return (0);
28+
return (n % sum == 0);
29+
}
30+
31+
/*
32+
** Little test
33+
*/
34+
int main(void)
35+
{
36+
int pretty = 0;
37+
38+
printf("Display all Harshad numbers between 1 and 200\n\n");
39+
for (int i = 0; i < 201; ++i)
40+
{
41+
if (isHarshad(i))
42+
{
43+
printf("%d", i);
44+
pretty++;
45+
if (pretty % 5 == 0)
46+
printf("\n");
47+
else
48+
printf("\t");
49+
}
50+
}
51+
printf("\n\n=== END ===\n");
52+
return (0);
53+
}

0 commit comments

Comments
 (0)