C Programme in happy number

Pls tell me why does my c programme does not give any output whenever i input some unhappy number.

#include <stdio.h>
//This is a program to check if a number is happy or not
int main()
{
	//code
int n,r,sum,tmp;  //n=number,r=remainder
printf("Enter a number:");
scanf("%d",&n);//input number
tmp=n;
 do
 { sum=0;
    while(tmp>0)
	{
	    r=tmp%10;
	    sum+=r*r;
	    tmp=tmp/10;
	}
	tmp=sum;

 }while(tmp>1);

    if(sum==1)
    {
        printf("%d is a happy number",n);
    }
    else
    {
        printf("%d is not a happy number",n);
    }
    return 0;
}

You have a while loop nested inside of a do while loop. Your while loop increases tmp while your do while loop will only break if tmp is less than 1. You have created an infinite loop.

Thank you @JeremyLT. I have solved this problem on yesterday itself.

1 Like