Given a starting number and the ending number the task is to find the all the Happy Number present in that given range and also to display it’s frequency.
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 HappyNumberRange
{
public static void main(String args[])
{
int start=0,end=0,frequency=0,r=0,s=0,temp=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a starting range: ");
start=sc.nextInt();
System.out.print("Enter a ending range: ");
end=sc.nextInt();
System.out.println("Happy Numbers in the given range are:-");
for(int i=start;i<=end;i++)
{
s=10;
temp=i;
while(s>9)
{
s=0;
while(temp>0)
{
r=temp%10;
s=s+(int)Math.pow(r,2);
temp=temp/10;
}
temp=s;
}
if(s==1)
{
System.out.println(i);
frequency++;
}
}
System.out.println("Frequency: "+frequency);
}
}Java