Write a C program to check whether the given number is Strong or not.
A strong Number is a number whose sum of factorial of its digits is equal to its original number. E.g 145
Steps to Check Strong Number in C
- Input a number.
- Calculate factorial of each of its digit and add them.
- If sum is equal to the original number,then its a strong number else not.
Check Whether a Number is Strong in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> #include <stdlib.h> int main() { int num, digit, sum=0, i, fact=1; printf("Enter a number \n"); scanf("%d",&num); int temp = num; //Copying original number while(temp>0){ digit = temp%10; //Calculating factorial of digit fact=1; for(i=1; i<=digit; i++){ fact = fact*i; } //Adding Factorial sum += fact; temp = temp/10; } if(sum == num) printf("Strong Number \n"); else printf("Not a Strong Number \n"); return 0; } |
Output
Print all Strong Numbers in the Interval
To do this we have to write the logic to check strong numbers in a separate function and then call that function for every number in the interval using for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> //Function return true if passed 'num' is Strong bool isStrong(int num){ int digit, sum=0, i, fact=1; int temp = num; //Copying original number while(temp>0){ digit = temp%10; //Calculating factorial of digit fact=1; for(i=1; i<=digit; i++){ fact = fact*i; } //Adding Factorial sum += fact; temp = temp/10; } return (sum == num); } int main() { int low, up, i; printf("Enter a Lower and upper Interval Value \n"); scanf("%d",&low); scanf("%d",&up); for(i=low; i<=up; i++){ if(isStrong(i)) printf("%d \n",i); } return 0; } |
Output
That’s all we need to write in C in order to check whether a number is a strong number or not. If you have any doubts share them in the comment section.