forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrong_number.c
57 lines (53 loc) · 1.09 KB
/
strong_number.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @file
* @brief Strong number is a number whose sum of all digits’ factorial is equal
* to the number n For example: 145 = 1!(1) + 4!(24) + 5!(120)
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
/**
* Check if given number is strong number or not
* @param number
* @return `true` if given number is strong number, otherwise `false`
*/
bool isStrong(int number)
{
if (number < 0)
{
return false;
}
int sum = 0;
int originalNumber = number;
while (originalNumber != 0)
{
int remainder = originalNumber % 10;
int factorial = remainder == 0 ? 0 : 1; /* 0! == 1 */
/* calculate factorial of n */
for (int i = 1; i <= remainder; factorial *= i, i++)
{
;
}
sum += factorial;
originalNumber /= 10;
}
return number == sum;
}
/**
* Test function
* @return void
*/
void test()
{
assert(isStrong(145)); /* 145 = 1! + 4! + 5! */
assert(!isStrong(543)); /* 543 != 5!+ 4! + 3! */
}
/**
* Driver Code
* @return None
*/
int main()
{
test();
return 0;
}