 # C Programming Code Examples

## C > For Loops and While Loops Code Examples

### check whether a number is Strong number or not

``` check whether a number is Strong number or not Write a C program to input number from user and check whether number is Strong number or not. How to check strong numbers using loop in C programming. What is Strong number? Strong number is a special number whose sum of factorial of digits is equal to the original number. For example: 145 is strong number. Since, 1! + 4! + 5! = 145 Input a number from user to check for strong number. Store this in a variable say j. Copy it to a temporary variable for calculations purposes, say originalNum = j. Initialize another variable to store sum of factorial of digits, say sum = 0. Find last digit of the given number j. Store the result in a variable say lastDigit = j % 10. Find factorial of lastDigit. Store factorial in a variable say fact. Add factorial to sum i.e. sum = sum + fact. Remove last digit from j as it is not needed further. Repeat steps 3 to 6 till j > 0. After loop check condition for strong number. If sum == originalNum, then the given number is Strong number otherwise not. #include <stdio.h> int main() { int i, originalNum, j, lastDigit, sum; long fact; /* Input a number from user */ printf("Enter any number to check Strong number: "); scanf("%d", &j); /* Copy the value of j to a temporary variable */ originalNum = j; sum = 0; /* Find sum of factorial of digits */ while(j > 0) { /* Get last digit of j */ lastDigit = j % 10; /* Find factorial of last digit */ fact = 1; for(i=1; i<=lastDigit; i++) { fact = fact * i; } /* Add factorial to sum */ sum = sum + fact; j = j / 10; } /* Check Strong number condition */ if(sum == originalNum) { printf("%d is STRONG NUMBER", originalNum); } else { printf("%d is NOT STRONG NUMBER", originalNum); } return 0; } ``` 