C Program To Check Strong Number or Not
Strong number is a special number whose sum of the factorial of digits
is equal to the original number.
Example:
Given Number : 145
It is a Strong Nuumber.
Since: 1! + 4! + 5! = 145
1! = 1*1 = 1
4! = 1*2*3*4 = 24
5! = 1*2*3*4*5 = 120
Example:
Given Number : 125
It is Not a Strong Nuumber.
Since:
1! + 2! + 5! = 123
1! = 1*1 = 1
2! = 1*2 = 2
5! = 1*2*3*4*5 = 120
you can use the following algorithm:
- Read the input number.
- Create a copy of the input number.
- Initialize a variable sum to 0.
- While the given number is not equal to 0,perform the following steps:
- Get the last digit of the num by using the modulo operator (%).
- Calculate the factorial of the last digit and add it to the sum.
- Divide the num by 10 to remove the last digit.
- Check if the sum is equal to the copy number.
- If they are equal, the number is a Strong Number.
Otherwise, it is not. - Print the result.
Source Code
// C Program To Check Strong Number or Not
#include<stdio.h>
#include<conio.h>
void main()
{
int num , Rd , i , fact , sum = 0 , ncopy ;
printf("Enter a Number To Check : ");
scanf("%d",&num);
ncopy=num;
while( num!=0 )
{
Rd = num % 10 ;
fact = 1;
for( i=1; i<=Rd; i++ )
{
fact = fact * i ;
}
sum = sum + fact ;
num = num / 10;
}
if ( sum==ncopy )
printf("It is a Strong Number . ");
else
printf("It is Not a Strong Number . ");
getch();
}
Output
Sample Input :
Enter a Number To Check : 145
Sample Output:
It is a Strong Number
Sample Input :
Enter a Number To Check : 123
Sample Output:
It is Not a Strong Number
In this algorithm, the input number is processed to calculate the sum of the factorials of its digits. A separate factorial function is used to calculate the factorial of a given number. The algorithm iterates through each digit of the number, calculates its factorial, and adds it to the sum. Finally, it checks if the sum is equal to the copy number to determine if it is a Strong Number.
Please note that this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.
Data Type | Variable Name | Description |
---|---|---|
int | num | To Store an Integer That You Want To Check |
int | Rd | To Store Last Digit of the Number During Loop |
int | i | To Control The For Loop |
int | fact | To Store Factorial of Right Digit |
int | sum | To Store sum of the factorials of digits |
int | ncopy | To Store Copy of Input Number |
Looping Related Programs
- C Program to Check Perfect Number or Not
- C Program to Check Prime Number or Not
- C Program to Check Twin Prime Number or Not
- C Program to Check Duck Number or Not
- C Program to Check Niven Or Harshad Number or Not
- C Program to Check Spy Number or Not
- C Program to Check Palindrome Number or Not
- C Program to Check Neon Number or Not
- C Program to Check Armstrong Number or Not
- C Program to Check Happy Number or Not
- C Program to Check Automorphic Number or Not