Write a program in java , to display all sunny numbers from 1 to 100
Answers
Answer:
Some of the other examples of happy numbers are 7, 28, 100, 320 and so on.
Unhappy number will result into a cycle of 4, 16, 37, 58, 89, 145, 42, 20, 4, ...
To find whether a given number is happy or not, calculate the square of each digit present in number and add it to a variable sum. If resulting sum is equal to 1 then, given number is a happy number. If the sum is equal to 4 then, the number is an unhappy number. Else, replace the number with the sum of the square of digits.
Algorithm
isHappyNumber() determines whether a given number is happy or not.
If the number is greater than 0, calculate remainder rem by dividing the number with 10.
Calculate square of rem and add it to a variable sum.
Divide number by 10.
Repeat the steps from a to c till the sum of the square of all digits present in number has been calculated.
Finally, return the sum.
To display all happy numbers between 1 and 100,
Start a loop from 1 to 100, then make a call to isHappyNumber() method for each value from 1 to 100 and store the return value into a variable result.
If the result is neither equal to 1 nor 4 then, make a call to isHappyNumber().
Otherwise, if the result is equal to 1, it implies that the number is happy and display it.
Explanation:
Answer:
class help
{
public void main( )
{
int a;
{
for (a=1;a <=100;a++)
System.out.println (a);
}
}
}