Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
strong_number.c File Reference

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>
Include dependency graph for strong_number.c:

Functions

bool isStrong (int number)
 Check if given number is strong number or not. More...
 
void test ()
 Test function. More...
 
int main ()
 Driver Code. More...
 

Detailed Description

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)

Function Documentation

◆ isStrong()

bool isStrong ( int  number)

Check if given number is strong number or not.

Parameters
number
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; /* 0! == 1 */
27
28 /* calculate factorial of n */
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()

int main ( void  )

Driver Code.

Returns
None
54{
55 test();
56 return 0;
57}
void test()
Test function.
Definition: strong_number.c:43
Here is the call graph for this function:

◆ test()

void test ( void  )

Test function.

Returns
void
44{
45 assert(isStrong(145)); /* 145 = 1! + 4! + 5! */
46 assert(!isStrong(543)); /* 543 != 5!+ 4! + 3! */
47}
bool isStrong(int number)
Check if given number is strong number or not.
Definition: strong_number.c:15
Here is the call graph for this function: