dimanche 12 juillet 2020

Why does the program mentioned below does not return anything whenever i enter some unhappy number for test?

Compiler stops working whenever i entered the unhappy number for test and does not return anything Here is the output: 1.When i enter the happy number enter image description here

2.when i enter the unhappy number then output screen even after 2 minutes of time enter image description here

#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;
}


Aucun commentaire:

Enregistrer un commentaire