Strong Number Program in C: A strong number is a number that is the sum of the factorial of its digits.
Examples:
- Assume the number is 145, which equals 1! + 4! + 5! = 1 + 24 + 120 = 145.
145 is a strong number since the sum of its factorials equals the number itself.
- Assume the number is 112, which equals 1! + 1! + 2! = 1 + 1 + 2 = 4
Here 4 is not equal to 112. So 112 is not a strong number.
Write a C Program that will ask the user for a number and then check whether the number is a strong number or not. Additionaly, create a program that finds the strong numbers in a specified range.
1. Take the number as input and store the number in a variable.
2. Extract the digits of the number and find the factorial of each digit.
3. Calculate the sum of the factorial of each digit.
4. If the sum is equal to the number, print “The number is a strong number”.
5. If the sum is not equal to the number, print “The number is not a strong number”.
There are several ways to write a strong number program in C language. Let’s look at the different techniques for writing a strong number program.
- Strong Number Program in C using While Loop
- Print Strong Numbers from 1 to n
- Find Strong Numbers in a Given Range
In this approach, we will use a loop to extract the digits of the number and find the factorial of each digit.
Methods used:
int factorial(int n): This function will find the factorial of a number.
Here is source code of the C Program to check whether a given number is strong number or not using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to Check whether a given Number is strong Number using while loop */ #include <stdio.h> int factorial(int n) { int i, fact = 1; for (i = 1; i <= n; i++) { fact = fact * i; } return fact; } int main(void) { int number, sum = 0; printf("Enter the number: "); scanf("%d", &number); int i = n; while (i != 0) { sum = sum + factorial(i % 10); i = i / 10; } if (sum == n) { printf("%d is strong number", number); } else { printf("%d is not a strong number", number); } }
1. In this C program, we are reading the integer value using ‘number’ variable.
2. The digits of the number are extracted and the factorial of each digit is found.
3. The sum of the factorial of each digit is calculated.
4. If the sum is equal to the number, print “Number is a strong number“.
5. If the sum is not equal to the number, print “Number is not a strong number“.
Time complexity: O(n)
Time complexity of this program is O(n). Here n is the number of digits in the number. The factorial part is neglected as only a single digit is take every time so the number of steps will be very less than what we get from digit extraction.
Space Complexity: O(1)
Space complexity of this program is O(1). Here we are not using any extra space.
Testcase 1: In this case, we enter the number “145” as input to determine whether it is a strong number or not.
Enter the number: 145 145 is a strong number
Testcase 2: In this case, we enter the number “112” as input to determine whether it is a strong number or not.
Enter the number: 112 112 is not a strong number
In this approach, we will print the strong numbers from 1 to n.
Methods used:
- long factorial(long n): This function will find the factorial of a number.
- long isStrong(long n): This function will check whether the number is strong or not.
- void printStrong(long n): This function will print the strong numbers from 1 to n.
Example:
Input: Enter the number: 5000
Output:
Strong Numbers are:
1
2
145
Here is source code of the C Program to print strong numbers from 1 to n. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to print strong numbers from 1 to n */ #include <stdio.h> long factorial(long n) { long i, fact = 1; for (i = 1; i <= n; i++) { fact = fact * i; } return fact; } long isStrong(long n) { long sum = 0; long i = n; while (i != 0) { sum = sum + factorial(i % 10); i = i / 10; } if (sum == n) { return 1; } else { return 0; } } void printStrong(long n) { long i; for (i = 1; i <= n; i++) { if (isStrong(i) == 1) { printf("%ld\n", i); } } } int main(void) { long n; printf("Enter the number: "); scanf("%ld", &n); printf("Strong Numbers are: "); printStrong(n); }
1. The program asks the user to enter n where n is the maximum limit.
2. In the printStrong function, we will loop through the numbers from 1 to n.
3. If the number is strong, then print it.
4. The printStrong function in turn calls the isStrong function to check whether the number is strong or not.
5. Inside the isStrong function, we will calculate the sum of the factorial of each digit.
6. If the sum is equal to the number, then 1 is returned. Otherwise 0 is returned.
7. The program will print the strong numbers from 1 to n.
Time complexity: O(n)
Time complexity of this program is O(n). A for loop goes from 1 to n, inside which is checking for every number whether it’s factorial is strong or not.
So if we consider the size of the range as n, the number of digits for every number as m and the factorial for every digit as f (ranging from 1 to 9), then the time complexity is O(n * m * f). But here as f is very limited in terms of values and m is also so long as the digit itself so n is the only factor which is considered. So the time complexity is O(n).
Space Complexity: O(1)
Space complexity of this program is O(1). Here we are not using any extra space.
Testcase 1: In this case, we enter “5000” as the n value in order to find all strong numbers up to 5000.
Enter the number: 5000 Strong Numbers are: 1 2 145
Testcase 2: In this case, we enter “100” as the n value in order to find all strong numbers up to 100.
Enter the number: 100 Strong Numbers are: 1 2
In this approach, we will ask the user for two limits and then print all the strong numbers between the limits.
Methods used:
- long factorial(long n): This function will find the factorial of a number.
- long isStrong(long n): This function will check whether the number is strong or not.
- void printStrong(long a, long b): This function will print the strong numbers between the limits.
Example:
Input: First limit: 1 and Second limit: 100000000
Output:
1 = 1! = 1
2 = 2! = 2
145 = 1! + 4! + 5! = 1 + 24 + 120 = 145
40585 = 4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40320 + 120 = 40585
Here is source code of the C Program to find strong numbers in a given range. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to find strong numbers in a given range */ #include <stdio.h> long factorial(long n) { long i, fact = 1; for (i = 1; i <= n; i++) { fact = fact * i; } return fact; } long isStrong(long n) { long sum = 0; long i = n; while (i != 0) { sum = sum + factorial(i % 10); i = i / 10; } if (sum == n) { return 1; } else { return 0; } } void printStrong(long a, long b) { long i; for (i = a; i <= b; i++) { if (isStrong(i) == 1) { printf("%ld\n", i); } } } int main(void) { long a, b; printf("Enter the first limit: "); scanf("%ld", &a); printf("Enter the second limit: "); scanf("%ld", &b); if (a > b) { a ^= b; b ^= a; a ^= b; } printf("The strong numbers between %d and %d are: \n", a, b); printStrong(a, b); }
1. In this program, we begin with asking the user for the first and second limits and store the numbers in a and b variables.
2. After finding the minimum and maximum of the limits, the program will print the strong numbers between the limits by sending them to the printStrong function.
3. The printStrong function in turn calls the isStrong function to check whether the number is strong or not.
4. Inside the isStrong function, we will calculate the sum of the factorial of each digit.
5. If the sum is equal to the number, then 1 is returned. Otherwise 0 is returned.
Time complexity: O(1)
Time complexity of this program is O(1). If we consider the size of the range as n, the number of digits for every number as m and the factorial for every digit as f (ranging from 1 to 9), then the time complexity is O(n * m * f). But here as f is very limited in terms of values and m is also so long as the digit itself so n is the only factor which is considered. So the time complexity is O(n). For an arbitrary large value of lower and upper limit with many digits, m can take larger value so the time complexity can be O(m). However, time complexity will be O(n) in most of the cases.
Space Complexity: O(1)
Space complexity of this program is O(1). Here we are not using any extra space.
Testcase 1: In this case, we enter the first limit as “1” and the second limit as “100000000” as input to find all strong numbers in the range.
Enter the first limit: 1 Enter the second limit: 100000000 The strong numbers between 1 and 100000000 are: 1 2 145 40585
Testcase 2: In this case, we enter the first limit as “1” and the second limit as “500” as input to find all strong numbers in the range.
Enter the first limit: 1 Enter the second limit: 500 The strong numbers between 1 and 500 are: 1 2 145
To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.
- Practice BCA MCQs
- Apply for C Internship
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books