Happy numbers are positive non-zero integer numbers. If we find the sum of the squares of every digit, repeat that process until the number equals 1(one). Otherwise, it is called Unhappy Number or Sad Number. So, in this article, we will discuss happy numbers in Java in detail.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsExamples of Happy Numbers are 1, 7, 10, 13, 19, 23, 28, 31, 32, etc.
Logic Behind Happy Numbers in Java:
23 is a Happy Number or Not. |
Example: 11 is a Happy Number or Not. |
||||||||||
Step1: 22+ 32= 13 |
Step1:12+ 12= 2 | ||||||||||
Step2: 12+ 32= 10 | Step2: 22= 4 |
||||||||||
Step3:12+ 02=1 | |||||||||||
Output: 1(one), So 23 is a Happy number. | Output: 4(four), So 11 is an Unhappy number. |
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.
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 variable called result and initialize it with the value of a number.Note:
In Happy Number, the number is not affected by inserting/deleting Zeros on any side.
If one number will be a Happy Number, i.e., a sequence of a number is happy. For Example, 23 is a Happy Number; it indicates the sequence of numbers like 13,10,1 should be a Happy Number.If finally, the sum of its digit’s squares equals 4(four), i.e., it is Unhappy.
ExamplesFollowing are the different examples of checking the happy numbers in Java.
We create a checkHappyNumber ( ) method to verify whether or not the given number will be a Happy Number.
Code:
import java.util.*; public class HappyNumber { public static int checkHappyNumber (int number) { int rem = 0, sum = 0; // calculate the sum of squares of each digits while(number > 0) { rem = number %10; sum = sum+(rem*rem); number = number/10; } return sum; } public static void main(String[] args) { // Take number from KeyBoard Scanner sc = new Scanner (System.in); System.out.print("Enter a non-zero Positive Number:"); int number = sc.nextInt( ); int result = number; while (result != 1 && result != 4) { result = checkHappyNumber(result); } if (result ==1) { System.out.println ("It is a Happy Number"); } else { System.out.println (" It is not a Happy Number"); } } }
Output:
Example #2
import java.util. *; public class Main { public static Boolean checkHappyNumber(int number) { Setdigits=new HashSet (); while(digits.add(number)) { int result = 0; while(number > 0) { result += Math.pow(number % 10, 2); number = number/10; } number = result; } return number == 1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println ("Enter a non-zero integer number :"); int number = sc.nextInt(); System.out.println(checkHappyNumber(number)?"It is a Happy Number":"It is an Unhappy Number"); } }
:
Example #3We create a checkHappyNumber ( ) method to verify that all the numbers between a range of numbers are Happy Numbers or Not and print the list of Happy Numbers.
import java.util.*; public class HappyNumber { public static int checkHappyNumber(int number) { int rem = 0,sum = 0; // calculate the sum of squares of digits while(number >0) { rem = number%10; sum = sum + (rem*rem); number = number/10; } return sum; } public static void main(String[] args) { // Take starting and ending number from keyboard Scanner sc = new Scanner(System.in); System.out.print("Enter the Starting Number:"); int i=sc.nextInt(); System.out.print("Enter the Ending Number:"); int j=sc.nextInt(); System.out.println("The happy numbers between "+i+" and "+j+" are: "); for (int x=i ; x <= j; x++) { int result = x; //Happy number always ends with 1 and unhappy number ends with 4 while(result != 1 && result != 4) { result = checkHappyNumber(result); } if(result == 1) System.out.print(x + ","); } } }
The above is the detailed content of Happy Numbers in Java. For more information, please follow other related articles on the PHP Chinese website!