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.

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

+6 votes
asked Sep 22, 2020 by Tarun Kumar Tarlana (350 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, 2020 by Peter Minarik (86,040 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, 2020 by Tarun Kumar Tarlana (350 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.
...