In the previous article, we have discussed Java Program to Check Duck Number
In this article we are going to understand what Happy number is and how we can check whether a number is Happy or not in Java with examples.
Java Program to Check Happy Number
Happy numbers are numbers which eventually reach 1 when replaced by the sum of the square of each digit. Those numbers that do not reach 1 are unhappy numbers (or here not happy numbers).
Example: 23 -> Happy number 133 -> Happy number 9 -> Not a Happy number
In the above examples the numbers 23 and 133 both are happy numbers while 9 is not.
Let’s see different ways to do it.
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Approach:
- We ask the user to enter a number which we store in an integer variable
num
. - We then use a while loop to iterate digits in the number and calculate the sum of their square.
- After coming out of loop if the resultant sum is 1 then the number is a happy number, else it is not.
Method-1:Java Program to Check Happy Number By Using Static Value
import java.util.Scanner; public class HappyNumber { public static void main(String args[]) { // A number is declared int num = 23; //Temp is used to preserve the original value int temp = num,sum = 0, remainder =1; //Loop to add the digits while (temp > 9) { //Loop to iterate the digits while (temp > 0) { remainder = temp % 10; sum = sum + (remainder * remainder); temp = temp / 10; } temp = sum; sum = 0; } if (temp == 1) { System.out.println(num+" is a Happy Number"); } else { System.out.println(num+" is Not a Happy Number"); } } }
Output: 23 is a Happy Number
Method-2:By Using User Input Value
import java.util.Scanner; public class HappyNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //Temp is used to preserve the original value int temp = num,sum = 0, remainder =1; //Loop to add the digits while (temp > 9) { //Loop to iterate the digits while (temp > 0) { remainder = temp % 10; sum = sum + (remainder * remainder); temp = temp / 10; } temp = sum; sum = 0; } if (temp == 1) { System.out.println(num+" is a Happy Number"); } else { System.out.println(num+" is Not a Happy Number"); } } }
Output: Enter a number : 23 23 is a Happy Number
Method-3:Java Program to Check Happy Number By Using User Defined Method
import java.util.Scanner; public class HappyNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //calling checkNumber() user defined method //to check the number is happy number or not. checkNumber(num); } public static void checkNumber(int num) { //Temp is used to preserve the original value int temp = num,sum = 0, remainder =1; //Loop to add the digits while (temp > 9) { //Loop to iterate the digits while (temp > 0) { remainder = temp % 10; sum = sum + (remainder * remainder); temp = temp / 10; } temp = sum; sum = 0; } if (temp == 1) { System.out.println(num+" is a Happy Number"); } else { System.out.println(num+" is Not a Happy Number"); } } }
Output: Enter a number : 23 23 is a Happy Number
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs: