Happy Numbers In Java
In this tutorial, we will learn what is happy number or sad number and what is the logic to find happy number in java.
Happy numbers are positive numbers. If we find the sum of the square of every digit, repeat that process until the numbers equal to 1 or 4. if number is equal to 1 . it is happy number Otherwise it is called Sad number.
For Example : we have a number 32 and we want to check 32 is happy number or not ?
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 and repeat the same process for new variable. 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.
Algorithm to find Happy Number In Java :
Step 1: To Enter a non-zero positive number from the keyboard and assign it to the variable called number.
Step 2: Calculate the remainder by dividing (%) the given number by 10 (%).
Step 3: Calculate the square of the remaining value and add it to a variable sum. Else, replace the number with the sum of the square of digits.
Step 4: To divide (/) the number by 10.
Step 5: Repeat step: 2 to step: 4 until we get the sum of the square of all digits of the given number.
Step 6: The final addition value is stored in the variable sum.
Step 7: To define a…