SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Happy Number Program in Java and Python

29 December 2012

ISC 2012 Question 10 solution to check whether a number is a happy number using recursive sum of squares of digits.

Question:

A happy number is a number in which the eventual sum of the square of the digits of the number is equal to 1.

Example 1

28 = (2)^2 + (8)^2 = 4 + 64 = 68 68 = (6)^2 + (8)^2 = 36 + 64 = 100 100 = (1)^2 + (0)^2 + (0)^2 = 1 + 0 + 0 = 1

Hence, 28 is a happy number.

Example 2

12 = (1)^2 + (2)^2 = 1 + 4 = 5

Hence, 12 is not a happy number.

Design a class Happy to check if a given number is a happy number. Some of the members of the class are given below:

Class name: Happy Data member / instance variable: n : stores the number Member functions: Happy() : constructor to assign 0 to n void getnum(int nn) : to assign the parameter value to n int sum_sq_digits(int x): returns the sum of the square of the digits of x, using recursive technique void ishappy() : checks if the given number is a happy number by calling sum_sq_digits(int) and displays a message

Specify the class Happy giving details of the constructor, void getnum(int), int sum_sq_digits(int) and void ishappy(). Also define a main() function to create an object and call the methods to check for a happy number.

Algorithm:

Step 1: Start.

Step 2: Define a class named Happy.

Step 3: Declare an integer instance variable n.

Step 4: In the constructor, initialize n to 0.

Step 5: In getnum(nn), assign nn to n.

Step 6: In sum_sq_digits(x), if x == 0, return 0.

Step 7: Otherwise, extract the last digit as d = x % 10.

Step 8: Return d * d + sum_sq_digits(x / 10).

Step 9: In ishappy(), calculate a = sum_sq_digits(n).

Step 10: While a > 9, replace a with sum_sq_digits(a).

Step 11: If a == 1, display that n is a Happy Number.

Step 12: Otherwise, display that n is not a Happy Number.

Step 13: In main(), accept a number, create an object, call getnum() and then call ishappy().

Step 14: Stop.

Explanation:

A happy number is checked by repeatedly replacing the number with the sum of the squares of its digits. If this repeated process eventually gives 1, the number is called happy. For example, 28 becomes 68, 68 becomes 100 and 100 becomes 1. Therefore 28 is a happy number. The program follows the same idea by calculating digit-square sums again and again.

The class Happy stores the number in the instance variable n. The constructor initializes it to 0, and the method getnum(int nn) assigns the user-entered value to n. This keeps input assignment separate from the checking logic, which is the style expected in the ISC class-design question.

The method sum_sq_digits(int x) is recursive. If x becomes 0, there are no more digits left, so it returns 0. Otherwise, the last digit is extracted using x % 10. The square of this digit is added to the result of calling the same method for x / 10, which removes the last digit. In this way, each recursive call handles one digit, and the final return value is the sum of the squares of all digits.

The ishappy() method first finds the sum of square digits of the original number. Then it repeats the same operation while the result has more than one digit. The original solution stops when the result becomes a single digit. If that digit is 1, the number is printed as a Happy Number. Otherwise, it is printed as not a Happy Number. This directly follows the method demonstrated in the question examples.

Java Program:

Java
/**
* The class Happy checks whether a given number is a happy number or not.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC 2012 Question 10
*/

import java.util.Scanner;

class Happy
{
    int n;

    Happy()
    {
        n = 0;
    }

    void getnum(int nn)
    {
        n = nn;
    }

    int sum_sq_digits(int x)
    {
        if(x == 0)
        {
            return 0;
        }
        else
        {
            int d = x % 10;
            return d * d + sum_sq_digits(x / 10);
        }
    }

    void ishappy()
    {
        int a = sum_sq_digits(n);

        // Repeat until the result becomes a single digit.
        while(a > 9)
        {
            a = sum_sq_digits(a);
        }

        if(a == 1)
            System.out.print(n + " is a Happy Number");
        else
            System.out.print(n + " is Not a Happy Number");
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        Happy ob = new Happy();

        System.out.print("Enter any number: ");
        int b = sc.nextInt();

        ob.getnum(b);
        ob.ishappy();
    }
}

Equivalent Python Program:

Python
# Program to check whether a number is a happy number.

class Happy:
    def __init__(self):
        self.n = 0

    def getnum(self, nn):
        self.n = nn

    def sum_sq_digits(self, x):
        # Recursive sum of squares of digits.
        if x == 0:
            return 0
        else:
            d = x % 10
            return d * d + self.sum_sq_digits(x // 10)

    def ishappy(self):
        a = self.sum_sq_digits(self.n)

        # Repeat until the result becomes a single digit.
        while a > 9:
            a = self.sum_sq_digits(a)

        if a == 1:
            print(self.n, "is a Happy Number")
        else:
            print(self.n, "is Not a Happy Number")


ob = Happy()
num = int(input("Enter any number: "))
ob.getnum(num)
ob.ishappy()

Output:

Enter any number: 28 28 is a Happy Number Enter any number: 12 12 is Not a Happy Number Enter any number: 68 68 is a Happy Number

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →