What are happy numbers?
According to Wikipedia...
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits,
and repeat the process until the number either equals 1 (where it will
stay), or it loops endlessly in a cycle which does not include 1. Those
numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers).
Using Turbo C compiler for the C programming language...
#include<stdio.h>
void happynumber(void);
main()
{
clrscr();
happynumber();
getch();
}
void happynumber(void)
{
int n,q,r,t=0,i;
scanf("%d",&n);
for(i=1;i<1000;i++)
{ t=0;
for(;;)
{
q=n/10;
r=n%10;
t=t+(r*r);
if(q==0)
{
n=t;
break;
}
n=q;
}
}
if(t==1)
{
printf("happy number");
}
else
{
printf("not");
}
}
No comments:
Post a Comment