Happy Number in Java? | How to check Number is Happy or UnHappy?

Number is Happy or Not

In this article, we discuss how to determine whether a number is a happy number or an Unhappy number in java.

First of all, you need to know about what is Happy number and what is Unhappy number. after then we learn how to find a happy number or unhappy number in java.

What is Happy Number in Java?

A happy number is those numbers. which replaces the number by the sum of squares of that number digits and repeats this process again and again. until the final result becomes 1, these numbers are called happy numbers.

Here is the Happy number example:

Now let’s check if 32 is a happy number or not?

"Happy Number in java"

Result: yes  user entered number is a happy number because the final result will come 1.

There are so many examples for happy numbers like 7,28,100 and so on these are the happy numbers.

Must Read:- How To Count The Number Of Bits?

What is an unhappy number in java?

During the process to find a happy number, if the sum of squares of that number’s digits will become endless. those types are unhappy numbers in java.

Here is the Unhappy Number Example:

let’s check if 16 is a happy number or not?

unhappy number in java

…. this process are comes under the endless cycle

That’s why the user entered number is an unhappy number.

Similarly, you can check if the user entered number is a happy or unhappy number. you just need to square each digit of that particular number, if the final result will be 1 then that number is a happy number else it is an unhappy number.

Java Program to check number is a happy or unhappy?

In this Program Also we can find how many happy numbers are in between 0 to user-entered numbers, and what is the sum of these happy numbers.

import java.util.Scanner;

public class HappyNos {
	static boolean ishappy(int n) {
		while (n > 9) {
			int sum = 0;
			while (n != 0) {
				int r = n % 10;
				sum = sum + r * r;
				n = n / 10;
			n = sum;
		return n == 1 || n == 7;

	static void happyno(int n) {
		System.out.println("Happy nos are");
		for (int i = 1; i <= n; i++) {
			if (ishappy(i)) {
				System.out.print(i + " ");

	static int counthappy(int n) {
		int count = 0;
		for (int i = 1; i <= n; i++) {
			if (ishappy(i)) {
		return count;

	static int sumhappy(int n) {
		int sum = 0;
		for (int i = 1; i <= n; i++) {
			if (ishappy(i)) {
				sum = sum + i;
		return sum;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the no");
		int n = sc.nextInt();
		boolean b = ishappy(n);
		if (b)
			System.out.println("Happy no");
			System.out.println("Not happy no");
		int cnt = counthappy(n);
		System.out.println("\ntotal happy are " + cnt);
		int sm = sumhappy(n);
		System.out.println("total of these happy are " + sm);


Enter the no 21
Not happy no
Happy nos are
1 7 10 13 19
total happy are 5
total of these happy are 50


1. How do you find a happy number?

Ans:- At the starting of the article I put some examples. using these methods you can find happy number and unhappy number in a mathematical way.

2. How to find a happy number in java using while loop?

Ans:- you can find your answer in the program, just try to understand algorithm of the program.

3. What is the use of a scanner class to find a happy number?

Ans: You can check our program we use the Scanner class in our program. and as you know scanner class is used to get the input from the user with the help of println method. without the scanner class how we can get the input from the user?

4. Find the happy number in java using for loop?

Ans:- as you see in the program, we use for loop in our source code 3 times with methods. with the help of for loop, we are finding how many happy numbers between the user entered number from 0.

Admin Words:-

hopefully, now you can understand the java code. in this code, I will try to clear you all concepts regarding the happy or unhappy numbers.

also, we learn what is a happy number, what is an unhappy number, how to find these numbers, and how many numbers are happy between user-entered number.

if you still getting any confusion related to this article please feel free to ask in the comment box. and if you want to give any suggestions for improvement please comment below in the comment box.

checkout our Programming online Quiz section for basic programs.

0 0 vote
Article Rating
Previous articleJava Program-How To Count The Number Of Bits?
Next articleWhat is Java, Full Introduction of Java for Beginners with Examples?
Notify of
Inline Feedbacks
View all comments