Table of Contents

ToggleHave you ever stumbled upon a number and thought, “What makes this number unique?” In the vast realm of number theory, there exists a category of numbers that, at first glance, might appear ordinary, but hide a fascinating secret! These are the *Strong Numbers*.

In this article we will deep dive into Strong Number In C and see how can we implement and check these in C programming.

## What is a Strong Number in C?

In C programming, a strong number (also known as a strong or digit factorial) is a number that has a special property. The property is that the sum of the factorial of its individual digits is equal to the number itself. To represent this mathematically:

A number n is called a Strong Number if it satisfies the following condition:

- ∑ d! = n
- Where: n is the original number
- d represents each digit in n
- d! is the factorial of each digit
- ∑ is the summation symbol, representing the sum of the factorial of each digit

### Examples of Strong Number

For example, consider the number 145:

The digits are 1, 4, and 5. Using factorial notation: 1! = 1, 4! = 24, 5! = 120

*∑ d! = 1! + 4! + 5! = 1 + 24 + 120 = 145*

*Since the sum ∑ d! = 145, which is equal to the original number n, 145 is a Strong Number.*

As another example, take 216:

Digits are 2, 1, and 6 where 2! = 2, 1! = 1, 6! = 720

*∑ d! = 2! + 1! + 6! = 2 + 1 + 720 = 723*

Here, ∑ d! = 723, which is not equal to the original n = 216. Therefore, 216 is not a Strong Number.

### Strong Numbers List

Strong Number |
---|

1 |

2 |

145 |

40,585 |

145,314 |

40,585,135 |

## Algorithm To Check If A Number Is A Strong Number Or Not In C

*Algorithm to check if number is strong or not:*

- Create a function “factorial” that takes an integer as input and returns the factorial of that integer.
- If the input is 0 or 1, return 1.
- Otherwise, calculate and return the product of all positive integers from 1 to the input number.

- Create a function “isStrongNumber” that takes an integer as input and returns a boolean (true or false) indicating whether the input number is a strong number.
- Initialize a variable “originalNum” to store the original input number.
- Initialize a variable “sum” to store the sum of factorials and set it to 0.
- While the input number is greater than 0, repeat the following steps:
- Get the last digit of the input number
**(digit = num % 10)**. - Calculate the factorial of the digit using the “factorial” function.
- Add the factorial to the “sum.”
- Remove the last digit from the input number
**(num = num / 10).**

- Get the last digit of the input number
- Check if “sum” is equal to “originalNum.”
- If they are equal, return true (the number is a strong number).
- Otherwise, return false (the number is not a strong number).

## Strong Number in C Program

```
#include <stdio.h>
// Function to calculate the factorial of a number
int factorial(int num) {
if (num == 0 || num == 1)
return 1;
else
return num * factorial(num - 1);
}
// Function to check if a number is a strong number
int isStrongNumber(int num) {
int originalNum = num; // Store the original input number
int sum = 0; // Initialize a variable to store the sum of factorials
// Loop through each digit of the input number
while (num > 0) {
int digit = num % 10; // Get the last digit of the input number
sum += factorial(digit); // Calculate the factorial of the digit and add it to the sum
num /= 10; // Remove the last digit from the input number
}
// Check if the sum of factorials is equal to the original number
return (sum == originalNum);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
// Call the isStrongNumber function to check if the input number is strong
if (isStrongNumber(num))
printf("%d is a strong number.\n", num);
else
printf("%d is not a strong number.\n", num);
return 0;
}
```

**Output**

```
Enter a number: 145
145 is a strong number.
```

**Dry Run**

Let’s dry run the `isStrongNumber`

function with the number `145`

.

**Input:** Number `num`

= 145

**Steps:**

- Start
`isStrongNumber`

function with`num`

= 145 `originalNum`

= 145,`sum`

= 0- Enter the while loop (because 145 > 0):
- Calculate
`digit`

as the last digit of`num`

:`digit`

= 145 % 10 =>`digit`

= 5 - Call the
`factorial`

function with`digit`

= 5. Since, factorial(5) = 120 - Add the result to
`sum`

:`sum`

= 0 + 120 =>`sum`

= 120 - Update
`num`

to move to the next digit:`num`

= 145 / 10 =>`num`

= 14

- Calculate
- Continue the while loop (because 14 > 0):
- Calculate
`digit`

:`digit`

= 14 % 10 =>`digit`

= 4 - Call the
`factorial`

function with`digit`

= 4. Since, factorial(4) = 24 - Update
`sum`

:`sum`

= 120 + 24 =>`sum`

= 144 - Update
`num`

:`num`

= 14 / 10 =>`num`

= 1

- Calculate
- Continue the while loop (because 1 > 0):
- Calculate
`digit`

:`digit`

= 1 % 10 =>`digit`

= 1 - Call the
`factorial`

function with`digit`

= 1. Since, factorial(1) = 1 - Update
`sum`

:`sum`

= 144 + 1 =>`sum`

= 145 - Update
`num`

:`num`

= 1 / 10 =>`num`

= 0

- Calculate
- Exit the while loop as
`num`

is now 0. - Check if
`sum`

(145) is equal to`originalNum`

(145):- Since they’re equal, the function returns
`true`

.

- Since they’re equal, the function returns

## Printing Strong Numbers Between 1 to N

To print the strong numbers between 1 to N, we will loop through 1 to N and for each number we will utilise above function to check if number is Strong or not.

```
#include <stdio.h>
// Function to compute the factorial of a number
int factorial(int num) {
if (num == 0 || num == 1)
return 1;
else
return num * factorial(num - 1);
}
// Function to check if a number is a strong number
int isStrongNumber(int num) {
int originalNum = num; // Store the original number
int sum = 0; // Initialize sum to 0
while (num > 0) {
int digit = num % 10;
sum += factorial(digit); // Compute factorial of the digit and add it to sum
num /= 10;
}
return (sum == originalNum); // If sum is equal to the original number, return 1 (true), otherwise return 0 (false)
}
int main() {
int N;
printf("Enter the value of N: ");
scanf("%d", &N);
printf("Strong numbers between 1 and %d are:\n", N);
for (int i = 1; i <= N; i++) {
if (isStrongNumber(i)) {
printf("%d ", i);
}
}
return 0;
}
```

**Output**

```
Enter the value of N: 500
Strong numbers between 1 and 500 are:
1 2 145
```

The above C code is used to find and display all Strong Numbers between 1 and a user-specified number ( N ). The program employs two key functions: `factorial()`

for calculating the factorial of a given number, and `isStrongNumber()`

for verifying if a number is a Strong Number. The `main()`

function initializes the program by asking the user to input ( N ), the upper limit for the search. It then iterates through all numbers from 1 to ( N ), utilising

to check each number’s “strength.” If the function returns true, the number is printed to the console as a Strong Number. The **isStrongNumber()**`isStrongNumber()`

function, in turn, deconstructs each number to its individual digits and calculates the sum of their factorials for comparison with the original number.

## Program to Find Strong Numbers in a Given Range l,r

To extend the code for a range (l) to (r), you just need to make slight modifications in the `main()`

function. Instead of asking the user for only the value of (N) and then checking from `1`

to (N), you’ll prompt the user for both (l) and (r) and then check from (l) to (r).

```
#include <stdio.h>
// Function to compute the factorial of a number
int factorial(int num) {
if (num == 0 || num == 1)
return 1;
else
return num * factorial(num - 1);
}
// Function to check if a number is a strong number
int isStrongNumber(int num) {
int originalNum = num; // Store the original number
int sum = 0; // Initialize sum to 0
while (num > 0) {
int digit = num % 10;
sum += factorial(digit); // Compute factorial of the digit and add it to sum
num /= 10;
}
return (sum == originalNum); // If sum is equal to the original number, return 1 (true), otherwise return 0 (false)
}
int main() {
int l, r;
printf("Enter the range (l, r): "); // Prompt user to input the starting and ending numbers of the range
scanf("%d %d", &l, &r);
printf("Strong numbers between %d and %d are:\n", l, r);
for (int i = l; i <= r; i++) { // Loop through numbers from l to r
if (isStrongNumber(i)) {
printf("%d ", i);
}
}
return 0;
}
```

**Output**

```
Enter the range (l, r): 100 2500
Strong numbers between 100 and 2500 are:
145
```

**Time and Space Complexity**

Let’s understand time and space complexity for above code.

- We loop through every number from (l) to (r): (O(r-l)).
- For each number, we loop through its digits. Let’s say a number has at most (d) digits (where (d) is the number of digits in (r)): (O(d)).
- For each digit, we calculate its factorial. Computing factorial for a digit (which ranges from 0 to 9) in a straightforward manner will take at most (O(9)), or simply (O(1)) since it’s a constant factor. However, if we were to compute factorial for larger numbers, it would take time proportional to the number, but given that we’re only computing factorial for single-digit numbers, it remains a constant time operation.

Given this, the total time complexity is:* [O((r-l) x d x 1) = O(d x r)]*

*Note: In practice, the constant factors might be significantly larger in the brute force solution than in the DP one, especially if the range from (l) to (r) includes many numbers.*

## Real World Application of Strong Numbers

Strong numbers, like many other special classes of numbers, are primarily of theoretical interest in number theory and recreational mathematics. That said, while strong numbers themselves might not find direct applications in practical real-world scenarios, the concepts and methods used to identify and work with them can be applied elsewhere. Here are a few ways in which they are tangentially related to real-world applications:

**Cryptography & Number Theory**: While strong numbers might not be directly used, number theory plays a pivotal role in modern cryptography. Concepts like prime numbers, factorization, and modular arithmetic are at the heart of many cryptographic algorithms that secure our online transactions and communications.**Mathematical Exploration**: The exploration of number properties, including strong numbers, can lead to the discovery of new mathematical theorems, patterns, or insights. Sometimes, such explorations can have unexpected applications in the future.**Educational Tools**: Strong numbers, along with other number types like prime numbers, Armstrong numbers, etc., are often used in educational settings to create puzzles, games, and challenges. This can help stimulate interest in mathematics and improve problem-solving skills.**Computational Efficiency**: The process of determining whether a number is strong requires the computation of factorials. Optimizing such computations can be of interest in areas where factorial calculations are crucial, such as in combinatorics and statistical mathematics.

## Key TakeAways

Here are the Key takeaways on Strong Numbers in C:

- A strong number is a number where the sum of the factorials of its digits is equal to the original number. For example, 145 is a strong number (1! + 4! + 5! = 145).
- To check if a number is strong in C, calculate the factorial of each digit, sum them up, and compare the sum to the original number. This can be done using loops and modular arithmetic.
- An efficient algorithm to check for strong numbers involves extracting digits, computing their factorials, summing up the factorials, and comparing the sum to the original number.
- Several techniques can be used to print strong numbers in a given range like brute force, pre-computing factorials, dynamic programming etc. Time complexity is O(d * r) for brute force.
- Strong numbers are mostly of theoretical and recreational interest in number theory and math. They provide good programming practice and build intuition related to cryptography, but may not have direct real-world applications.

Checkout more C Tutorials here.

**I hope You liked the post ?. For more such posts, ? subscribe to our newsletter.** Try out our **free resume checker** service where our * Industry Experts* will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.

## FAQ

Can any number be a strong number?

**Answer**: No, not every number is a strong number. Strong numbers are relatively rare and specific. Most numbers do not satisfy the condition of having the sum of their digit factorials equal to themselves.

How do you calculate the factorial of a number?

**Answer**: To calculate the factorial of a non-negative integer n, you multiply all positive integers from 1 to n together. For example, 5! (read as “5 factorial”) is equal to 5 x 4 x 3 x 2 x 1, which equals 120.

Are there any well-known strong numbers other than 145?

**Answer**: Yes, 145 is the most well-known strong number. However, there are other strong numbers, such as 40,585 which is the second smallest strong number. Strong numbers are relatively rare, and there are only a few known examples.

How can I find strong numbers?

**Answer:** Finding strong numbers typically involves writing a program or using a computer algorithm to check numbers one by one. There is no known efficient formula for generating strong numbers, so it often involves a brute-force search.