Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

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

–2 votes
asked Jun 7, 2018 by KAUSTAV (720 points)
closed Jun 30, 2018 by KAUSTAV
#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

1 Answer

0 votes
answered Jun 7, 2018 by anonymous
selected Jun 10, 2018 by KAUSTAV
 
Best answer

 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 KAUSTAV (720 points)
Thanks a lot sir, i really appreciate your help.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...