Featured
- Get link
- X
- Other Apps
C++ program to check happy number
/*Following program checks whether a number is happy number or not. Happy Number Example: 19. If the number's digits squared and added till we get 1, the number is happy number*/
#include<iostream.h>
#include<math.h>
long func(long);
void main()
{
long a;
cout<<"\tEnter a number\n";
cin>>a;
long y=func(a);
if(y==1)
cout<<"Happy Number";
else
cout<<"Unhappy Number\n";
}
long func(long x)
{
long sum=0,rem;
while(x>0)
{
rem=x%10;
sum=sum+pow(rem,2);
x=x/10;
}
if(sum>10)
func(sum);
else
return sum;
}
OUTPUT:
#include<iostream.h>
#include<math.h>
long func(long);
void main()
{
long a;
cout<<"\tEnter a number\n";
cin>>a;
long y=func(a);
if(y==1)
cout<<"Happy Number";
else
cout<<"Unhappy Number\n";
}
long func(long x)
{
long sum=0,rem;
while(x>0)
{
rem=x%10;
sum=sum+pow(rem,2);
x=x/10;
}
if(sum>10)
func(sum);
else
return sum;
}
OUTPUT:
- Get link
- X
- Other Apps
Labels:
C++ program to check happy number
happy number program in C++
how to check happy number in c++
Comments
Popular Posts
C++ program to display numbers which get reversed after multiplying by 4
- Get link
- X
- Other Apps
Armstrong Program in C++
ReplyDeleteArmstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself.
For example 153 is armstrong number, 132 is not prime number. Armstrong program in c++ is very simple and easy to write.
happy valentines images
ReplyDeletepromise images
kiss day in india
promise day wallpaper
kiss day msg
Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards, Vistara customer care
ReplyDelete