to check given number is magic or not, what wrong in my code? in c

+6 votes
asked Sep 22 by Tarun Kumar Tarlana (200 points)
#include <stdio.h>
int rnum(int number);
int main()
{int num,temp,x,sum1=0,sum2;
    printf("Hello World\nenter the number ");
    scanf("%d",&num);
    temp=num;
    while(temp>0)
    {
        x=temp%10;
        sum1+=x;
        temp=temp/10;
        
    }printf("%d",sum1);
    
    sum2=rnum(sum1);
    if(num==sum1*sum2)
    {
        printf("%d is a magic number",num);
    }
    return 0;
}

    int rnum(int number)
    {int rnumber=0,x;
    while(number>0)
    {   x=number%10;
        rnumber=rnumber*10+x;
        number/10;
        
    }printf("%d",rnumber);
    return rnumber;
    }

1 Answer

–1 vote
answered Sep 22 by Peter Minarik (12,220 points)

First of all, it's nice to describe what your problem is.

Second, it's also nice to explain what your program is supposed to do. In this instance, explain the definition of what you mean by "magic number".

I've found a problem in rnum() where you can easily end up in an infinite loop:

number/10;

I think what you wanted to write was:

number /= 10; // i.e. number = number / 10;

Now your code runs, not ending up in an infinite loop.

commented Sep 22 by Tarun Kumar Tarlana (200 points)
thanks brother
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.
...