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)
More...
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
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)
◆ isStrong()
bool isStrong |
( |
int |
number | ) |
|
Check if given number is strong number or not.
- Parameters
-
- Returns
true
if given number is strong number, otherwise false
16{
17 if (number < 0)
18 {
19 return false;
20 }
21 int sum = 0;
22 int originalNumber = number;
23 while (originalNumber != 0)
24 {
25 int remainder = originalNumber % 10;
26 int factorial = remainder == 0 ? 0 : 1;
27
28
29 for (int i = 1; i <= remainder; factorial *= i, i++)
30 {
31 ;
32 }
33 sum += factorial;
34 originalNumber /= 10;
35 }
36 return number == sum;
37}
◆ main()
Driver Code.
- Returns
- None
54{
56 return 0;
57}
void test()
Test function.
Definition: strong_number.c:43
◆ test()
Test function.
- Returns
- void
44{
47}
bool isStrong(int number)
Check if given number is strong number or not.
Definition: strong_number.c:15