STRONG NUMBER PROGRAM IN C
In this example , you will learn a program to check whether entered number is strong number or not.
Strong number:
If sum of factorials of digits of a number is equal to the original number, then it is called as Strong number.
Let's consider one example,
suppose we have to check whether 145 is strong number or not .
1! + 4!+ 5!
= 1 + 24 + 120
= 145
i.e. equal to the original number.
Hence, 145 is a strong number.
Algorithm to check for strong number:
1. Input any number from user to check for strong number.
2. Initiailize one variable sum to store the sum of factorials of digits.
3. Find last digit of number by using modulo operator (%).
4. Find the factorial of last digit and add it to the sum.
5. Then remove the last digit from the number as it is not required further.
6. Repeat steps 3-6 until number >0.
7. Check whether sum is equal to the number or not. If sum is equal to entered number then it is strong number otherwise it is not a strong number.
Program to check for strong number in C:
#include<stdio.h>
int main( )
{
int i, num, temp, rem, sum=0, fact=1;
printf("Enter the number :");
scanf("%d",&num);
temp=num;
while(num>0)
{
rem=num%10;
for(i=1;i<=rem;i++)
{
fact=fact*i;
}
sum= sum+ fact;
num=num/10;
}
num=temp;
if(sum==num)
{
printf("%d is strong number",num);
}
else
{
printf("%d is not a strong number",num);
}
return 0;
}
Output:
145 is Strong number
Enter the number:179
179 is not Strong number
So that's it ! Just try this code by your own. If you have any doubts then feel free to drop a comment . I'll be happy to answer your questions .
Comments
Post a Comment