Happy Number is a number whose eventual sum of square of it’s digits is equal to 1.
Example 1: 19. 12+92 = 82. 82+22 = 68. 62+82 = 100. 12+02+02 = 1. Hence it is a Happy Number.
Java
import java.util.*;
public class HappyNumber
{
public static void main(String args[])
{
int n=0,s=0,r=0,nCopy=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
n=sc.nextInt();
s=10;
nCopy=n;
while(s>9)
{
s=0;
while(n>0)
{
r=n%10;
s=s+(int)Math.pow(r,2);
n=n/10;
}
n=s;
}
if(s==1)
{
System.out.println(nCopy+" is a Happy Number");
}
else
{
System.out.println(nCopy+" is not a Happy Number");
}
}
}Java