Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
What is Strong Number in C?
3.
Examples of Strong Number
3.1.
Example 1
3.2.
Example 2
4.
Algorithm to Check If a Number is a Strong Number or Not in C
5.
Program to Check Whether the Number is a Strong Number or Not.
5.1.
1. Using Functions
5.2.
C
5.2.1.
 
5.3.
2. Using For Loops
5.4.
C
5.4.1.
 
5.5.
3. Using While Loop
5.6.
C
5.6.1.
 
6.
Program to Print the Strong Numbers from 1 to n
6.1.
C
7.
Program to Find Strong Numbers in a Given Range l,r
7.1.
C
7.1.1.
 
8.
Frequently Asked Questions
8.1.
What is strong number? 
8.2.
What is strong number in C using for loop?
8.3.
Is 871 is a strong number?
8.4.
What is a strong number in C Plus Plus? 
8.5.
Is 145 a strong number? 
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Strong Number In C

Author Vivek Tiwari
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction 

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.

strong number

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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;
}

 

Output

output

Explanation

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;
}

 

Output

output

Explanation

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;
}

 

Output

output

Explanation

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;
}

 

Output

output

Explanation

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;
}

 

Output

output

Explanation

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 our Guided path for DSA, which is free! 

Also read reverse a number.

Thank you for reading. Until then, Keep Learning and Keep Coding.

Keep coding
Previous article
The Number Triangle
Next article
Ceil Function in C
Live masterclass