66 960
Assignments Done
99,2%
Successfully Done
In November 2018

Answer to Question #15626 in Java | JSP | JSF for dave

Question #15626
Happy Numbers.. Java Program

A happy number is built by adding the squares of its digits, doing this permanently, the numbers will end in 1 or 4.

if a positive integer ends with 1, then it is a happy number.
the first few happy numbers are: 1,7,10,13,19,23,28 ...

solution : 13 = 1^2 +3 ^2 = 1+9 = 10
10 = 1^2 + 0+2 = 1+0 = 1

how many positive happy numbers are there in less than 300,000?
Expert's answer
public class main {

/**
* @param args
*/
public static void
main(String[] args)
{
int count=0;
for(int
i=1;i<300000;i++)
{
int
var=i;

var=getSqSumm(var);
while(var>9)
{
var=getSqSumm(var);
}

if(var==4||var==1)
{
count++;
}
}

System.out.println(count);
}

public
static int getSqSumm(int numb)
{
int var=numb;
int
res=0;
while(var>0)
{
res+=Math.pow(var%10,2);
var/=10;
}
return
res;
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions