# the following code doesn't show if a number is strong number or not, where did i make a mistake?

asked Jun 7, 2018
closed Jun 30, 2018
#include <stdio.h>

int main()
{
int n,i,r,sum =0,fact =1,temp;
printf("Enter number = ");
scanf("%d",&n);
temp = n;
while(n>0)
{
r = n%10;
for(i=r;i>=1;i--)
{
fact = fact*i;
}
sum = sum + fact;
n= n/10;
}
n=temp;
if(n==sum)
printf("strong number");
else
printf("not a strong number");
}
closed with the note: the answer by the below user is sufficient

answered Jun 7, 2018 by anonymous
selected Jun 10, 2018 by KAUSTAV

fact=1;
for(i=r;i>=1;i--)
{
fact = fact*i;
}

When calculate factorial then initial value of fact change so when loop move next step then fact value store previous, first define initial value of fact just before for loop so fact value not change and it is always 1. /

#include <stdio.h>

int main()
{
int n,i,r,sum =0,fact ,temp;
printf("Enter number = ");
scanf("%d",&n);
temp = n;
while(n>0)
{
r = n%10;

fact=1;
for(i=r;i>=1;i--)
{
fact = fact*i;
}
sum = sum + fact;
n= n/10;
}
n=temp;
if(n==sum)
printf("strong number");
else
printf("not a strong number");
}

commented Jun 7, 2018 by (720 points)
Thanks a lot sir, i really appreciate your help.