When the sum of the factorial of each individual digit is equal to an original number then a number is said to be a strong number. For example 145=1! + 4! + 5! are Strong numbers.
List of Strong number are: 1, 2, 145, 40585, ..
In this program, you will write a C program to check whether the number entered by the user is a Strong number or not.
#include <stdio.h>
int main()
{
int remainders,fact,sum=0,i,num,copynum;
printf("Enter the number:");
scanf("%d",&num);
copynum=num;
while(num!=0)
{
remainders=num%10;
fact=1;
for(i=1;i<=remainders;i++)
{
fact=fact*i;
}
sum=sum+fact;
num=num/10;
}
if(sum==copynum)
{
printf("%d is strong numbers",copynum);
}
else
{
printf("%d is not a strong numbers",copynum);
}
return 0;
}
Enter the number:145 145 is strong numbers
num.num into another variable copynum for verification purposes i.e copynum=num.num and stored the last digit value in remainders variables i.e remainder=num%10.remainders value and store it in variable fact.
for(i=1;i<=remainders;i++)
{
fact=fact*i;
}
sum=sum+factnum variable as it is not needed anymore. num=num/10num is 0.copynum is the same as the sum value i.e sum==copynum. if it is the same, then the number is strong otherwise it is not a strong number.
#include <stdio.h>
int main()
{
int remainders,sum,fact,range,i,num,n;
printf("\n Enter the range:");
scanf("%d",&range);
printf("\n strong number from 1 to n are:");
for(n=1;n<=range;n++)
{
sum=0;
num=n;
while(num>0)
{
remainders=num%10;
fact=1;
for(i=1;i<=remainders;i++)
{
fact=fact*i;
}
sum=sum+fact;
num=num/10;
}
if(sum==n)
{
printf("\t%d",n);
}
}
return 0;
}
#include <stdio.h>
int main()
{
int remainders,sum,fact,lower,upper,i,num,n;
printf("Enter the lower range:");
scanf("%d",&lower);
printf("Enter the upper range:");
scanf("%d",&upper);
printf("strong number from %d to %d are:",lower,upper);
for(n=lower;n<=upper;n++)
{
sum=0;
num=n;
while(num>0)
{
remainders=num%10;
fact=1;
for(i=1;i<=remainders;i++)
{
fact=fact*i;
}
sum=sum+fact;
num=num/10;
}
if(sum==n)
{
printf("\t%d",n);
}
}
return 0;
}
Enter the lower range:2 Enter the upper range:400 strong number from 2 to 400 are: 2 145