C PROGRAM FOR HAPPY NUMBER OR NOT
Consider the following I/O samples.
SAMPLE INPUT: 5544
SAMPLE OUTPUT: NOT A HAPPY NUMBER
A number is called a HAPPY NUMBER is it leads to 1 after a sequence of steps i.e., by performing the sum of squares of its digits.
32 = 3^2 + 2^2 =13
13 = 1^2 + 3^3 = 10
10 = 1^2 + 0^2 = 1
C PROGRAM:#include<stdio.h>int main()
{
int n,sum=0,rem=0;
scanf("%d",&n);
printf("\n");
do
{
sum=0;
while(n)
{
rem=n%10;
sum=sum+(rem*rem);
n=n/10;
}
n=sum;
}
while(sum>9);
if(sum==1)
printf("HAPPY NUMBER");
else
printf("NOT A HAPPY NUMBER");
return 0;
}
OUTPUT:The given source code in C program "FOR HAPPY NUMBER OR NOT" is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free.If you have any feedback about this article and want to improve this, please comment in the comment section.
Comments
Post a Comment