This blog will discuss the approach to checking whether the given number is a strong number. Before understanding the approach and code, let's first discuss a strong number in C.

What is Strong Number in C?

A strong number is a positive integer whose sum of the factorials of its digits equals the original number. in simple terms a number is considered as a strong number if you take each digit, calculate its factorial, and sum of all those factorials is same as the original number. Few examples of strong numbers are1,2,145 and 40585.

To check if a number is strong in C, we can calculate the factorial of each digit of the number, add them up, and then compare the result with the original number.

Strong numbers are special and unique in that they are the only ones for which the sum of each digit's factorials matches the actual number.

We will discuss examples in the below section to understand strong numbers more clearly.

Examples of Strong Number

Example 1

Input:

145

Output:

145 is a Strong number

Explanation: 1!+4!+5! =1+24+120 =145

Since the sum of the factorial of all digits is equal to the number itself, hence it is a Strong number.

Example 2

Input:

140

Output:

140 is not a Strong number

Explanation: 1!+4!+0! =1+24+1 =26

Since the sum of the factorial of all digits is not equal to the number itself, hence it is not a Strong number.

Algorithm to Check If a Number is a Strong Number or Not in C

The algorithm to check if a number is strong or not in C is as follows:

Read the input value which is entered by the user

Declare variables like â€˜numâ€™, â€˜sumâ€™, â€˜tempâ€™, and â€˜factorialâ€™

Initialize the sum of factorials as 0

Now, create a loop till the input number â€˜numâ€™ becomes 0

Extract the last digit of â€˜numâ€™ using the modulo operator and store it in the temp variable

Calculate the factorial of the value stored in â€˜tempâ€™ and store the result in the â€˜factorialâ€™ variable

Add the value of â€˜factorialâ€™ to the â€˜sumâ€™ variable

Divide the value of â€˜numâ€™ by 10 and remove the last digit

Compare the sum of factorial â€˜sumâ€™ with the original input number

If â€˜sumâ€™ is equal to the input number then it will return that it is a strong number

If â€˜sumâ€™ is not equal to the input number then it will return that it is not a strong number

Program to Check Whether the Number is a Strong Number or Not.

Program in C to check whether a number is strong or not using functions.

1. Using Functions

Code

C

C

// function to calculate the factorial int fact(int n) { int f = 1; for (int i = 1; i <= n; i++) { f *= i; } return f; }

//function to check if a number is a strong number or not int isStrongNumberOrNot(int n) { int num = n; int sum = 0; int temp; while (n != 0) { temp = n % 10; sum += fact(temp); n /= 10; }

//check the condition if (sum == num) {

// return 1 if it is a strong number return 1; } else {

// return 0 if it not a strong number return 0; } }

int main() { int n;

//enter the number printf("Enter the number to check if the number is strong or not: "); scanf("%d", & n);

//check the condition if (isStrongNumberOrNot(n)) { printf("%d is a strong number.\n", n); } else { printf("%d is not a strong number.\n", n); } return 0; }

In this code, we have used functions to check whether a number is strong. There is an â€˜isStrongNumberOrNotâ€™ function used to check whether a given number is strong. It nitrates over the digit of the given number and calculates the factorial of it. Then the factorials obtained are summed up together. Then we will check if the sum is equal to the original number; it will return one, which indicates that the number is strong; if it returns 0, it suggests that the number is not strong.

2. Using For Loops

Code

C

C

//function to check whether the number is a strong number or not int isStrongNumberOrNot(int n) { int number = n; int sum = 0;

//Iterate over the digits of the number for (int i = n; i != 0; i /= 10) { int d = i % 10; int f = 1;

//calculate the factorial for (int j = 1; j <= d; j++) { f *= j; } sum += f; }

//check the conditions if (sum == number) {

// return 1 if it is a strong number return 1; } else {

// return 1 if it is not a strong number return 0; } }

int main() { int n; printf("Enter the number to check if the number is strong or not: "); scanf("%d", & n);

if (isStrongNumberOrNot(n)) { printf("%d is a strong number.\n", n); } else { printf("%d is not a strong number.\n", n); } return 0; }

In this code, we have used for loops. We have used the â€˜isStrongNumberOrNotâ€™ function to check whether a given number is strong. We have used the â€˜forâ€™ loop to iterate through the digits of the number. Inside the loop, we extract each digit to calculate the factorial of that digit using another â€˜forâ€™ loop and then update the sum variable. Then we will check if the sum is equal to the original number; it will return one, which indicates that the number is strong; if it returns 0, it suggests that the number is not strong.

3. Using While Loop

Code

C

C

#include <iostream>

using namespace std;

int isStrongNumberOrNot(int n) { int number = n; int sum = 0;

//itrate over the digits while (n != 0) { int d = n % 10; int f = 1;

//calculate the factorial of the digit for (int i = 1; i <= d; i++) { f *= i; } sum += f; n /= 10; } if (sum == number) {

// return 1 if the it is a strong number return 1; // Strong number } else {

// return 0 if it is not a strong number return 0; } } int main() { int n; printf("Enter the number to check if the number is strong or not: "); scanf("%d", & n); if (isStrongNumberOrNot(n)) { printf("%d is a strong number.\n", n); } else { printf("%d is not a strong number.\n", n); } return 0; }

In this code, we have used a while loop. The â€˜isStrongNumberOrNotâ€™ function is used to check whether the number is strong or not. We have used the while loop to iterate through the digit the number and for loop to calculate the factorial of that digit and then update the sum variable. Then we will check if the sum is equal to the original number; it will return one, which indicates that the number is strong; if it returns 0, it suggests that the number is not strong.

Program to Print the Strong Numbers from 1 to n

C

C

#include <iostream>

using namespace std;

// Function to calculate the factorial of a number int factorial(int num) { int fact = 1; for (int i = 1; i <= num; i++) { fact *= i; } return fact; }

// Function to check if a number is a strong number or not bool isStrong(int num) { int sum = 0; int temp = num; while (temp != 0) { int rem = temp % 10; sum += factorial(rem); temp /= 10; } return (sum == num); }

// Function to print all the strong numbers from 1 to n void printStrong(int n) { cout << "Strong numbers from 1 to " << n << " are: "; for (int i = 1; i <= n; i++) { if (isStrong(i)) { cout << i << " "; } } }

// Main function int main() { int n; cout << "Enter a positive integer: "; cin >> n; printStrong(n); return 0; }

The â€˜isStrongâ€™ function checks whether a given number is strong in this code. It uses a loop to extract each digit of the number and then add it to the sum variable. If the sum variable is equal to the original number, it will return true; otherwise, it will return false. The â€˜printStrongâ€™ function takes input from the user â€˜nâ€™ and prints all the strong numbers from 1 to n. It calls the â€˜isStrongâ€™ function to check whether each number in the range is strong. If a number is strong, it gets printed.

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

C

C

#include <iostream>

using namespace std;

int factorial(int n) { int fact = 1; for (int i = 2; i <= n; i++) { fact *= i; } return fact; }

bool isStrong(int n) { int sum = 0; int temp = n; while (temp > 0) { int digit = temp % 10; sum += factorial(digit); temp /= 10; } return sum == n; }

void findStrongNumbers(int l, int r) { cout << "Strong numbers between " << l << " and " << r << " are: ";

for (int i = l; i <= r; i++) { if (isStrong(i)) { cout << i << " "; } } }

int main() { int l, r; cout << "Enter the lower and upper limits: "; cin >> l >> r; findStrongNumbers(l, r); return 0; }

In this code, users input the range of numbers. The 'isStrong' function checks whether or not each number in the content is strong. The 'findStrongNumber' function takes two integers, 'l' and 'r,' as input to indicate the upper and lower limit of the range. In the main function, the user enters the range value. The function' findStrongNumbers' is used with the provided lower and upper limits as parameters to discover and display all the strong numbers within that specified range.

Frequently Asked Questions

What is strong number?

In mathematics, a number is said to be strong number, if it is a unique number whose factorial sum of its constituent digits is equal to the number itself.

What is strong number in C using for loop?

In C, you would compute the factorial of each digit and add them together to find a strong number using a for loop. It is a strong number if the outcome is the same as the starting value.

Is 871 is a strong number?

If we calculate the factorial sum of digits of 871, we get that factorial sum of its digits (8! + 7! + 1!) does not equal to 871, hence we can say 871 is not a strong number.

What is a strong number in C Plus Plus?

Similar to C, a strong number in C++ is identified by computing the factorial of each digit and verifying that the sum equals the starting value. In such case, it's a solid number.

Is 145 a strong number?

A strong number has a sum of factorials of its digits equal to the number itself. In the case of 145, the sum of the factorials of its digits (1! + 4! + 5!) equals 145 thus making it a strong number.

Conclusion

In this article, we discussed the problem Strong Number in C in which we are given a number â€˜nâ€™ and need to check whether the given number is a Strong Number. We hope you understood the problem and solution properly. Now you can do more similar questions. If you are a beginner, interested in coding, and want to learn DSA, you can look for ourGuided path for DSA, which is free!