TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to determine whether a given number is a happy number

Program to determine whether a given number is a happy number on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Program to determine whether a given number is a happy number

Explanation

In this program, we need to determine whether a given number is a happy number or not.

Happy number

The happy number can be defined as a number which will yield 1 when it is replaced by the sum of the square of its digits repeatedly. If this process results in an endless cycle of numbers containing 4, then the number is called an unhappy number.

For example, 32 is a happy number as the process yields 1 as follows

32 + 22 = 13
12 + 32 = 10
12 + 02 = 1

Some of the other examples of happy numbers are 7, 28, 100, 320 and so on.

The unhappy number will result in a cycle of 4, 16, 37, 58, 89, 145, 42, 20, 4, ....

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 sum. 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. Else, replace the number with the sum of the square of digits.

Algorithm

  1. isHappyNumber() determines whether a given number is happy or not.
    1. If the number is greater than 0, then calculate remainder rem by dividing the number with 10.
    2. Calculate square of rem and add it to a variable sum.
    3. Divide number by 10.
    4. Repeat the steps from a to c till the sum of the square of all digits present in number has been calculated.
    5. Finally, return the sum.
  2. Define and initialize variable num.
  3. Define a variable result and initialize it with a value of num.
  4. If the result is neither equal to 1 nor 4 then, make a call to isHappyNumber().
  5. Otherwise, if the result is equal to 1 then, given number is a happy number.
  6. If the result is equal to 4 then, given number is not a happy number.

Solution

Python

#isHappyNumber() will determine whether a number is happy or not
def isHappyNumber(num):
    rem = sum = 0;
    
    #Calculates the sum of squares of digits
    while(num > 0):
        rem = num%10;
        sum = sum + (rem*rem);
        num = num//10;
    return sum;
    
num = 82;
result = num;
 
while(result != 1 and result != 4):
    result = isHappyNumber(result);
 
#Happy number always ends with 1
if(result == 1):
    print(str(num) + " is a happy number");
#Unhappy number ends in a cycle of repeating numbers which contain 4
elif(result == 4):
    print(str(num) + " is not a happy number");   

Output:

82 is a happy number

C

#include <stdio.h>
 
//isHappyNumber() will determine whether a number is happy or not
int isHappyNumber(int num){
    int rem = 0, sum = 0;
    
    //Calculates the sum of squares of digits
    while(num > 0){
        rem = num%10;
        sum = sum + (rem*rem);
        num = num/10;
    }
    return sum;
}
    
int main()
{
    int num = 82;
    int result = num;
    
    while(result != 1 && result != 4){
        result = isHappyNumber(result);
    }
    
    //Happy number always ends with 1
    if(result == 1)
        printf("%d is a happy number", num);
    //Unhappy number ends in a cycle of repeating numbers which contains 4
    else if(result == 4)
        printf("%d is not a happy number", num); 
 
    return 0;
}

Output:

82 is a happy number

JAVA

public class HappyNumber
{   
    //isHappyNumber() will determine whether a number is happy or not
    public static int isHappyNumber(int num){
        int rem = 0, sum = 0;
        
        //Calculates the sum of squares of digits
        while(num > 0){
            rem = num%10;
            sum = sum + (rem*rem);
            num = num/10;
        }
        return sum;
    }
    
    public static void main(String[] args) {
        int num = 82;
        int result = num;
        
        while(result != 1 && result != 4){
            result = isHappyNumber(result);
        }
        
        //Happy number always ends with 1
        if(result == 1)
            System.out.println(num + " is a happy number");
        //Unhappy number ends in a cycle of repeating numbers which contains 4
        else if(result == 4)
            System.out.println(num + " is not a happy number");   
    }
}

Output:

82 is a happy number

C#

using System;
                    
public class HappyNumber
{
    //isHappyNumber() will determine whether a number is happy or not
    public static int isHappyNumber(int num){
        int rem = 0, sum = 0;
        
        //Calculates the sum of squares of digits
        while(num > 0){
            rem = num%10;
            sum = sum + (rem*rem);
            num = num/10;
        }
        return sum;
    }
    
    public static void Main()
    {
        int num = 82;
        int result = num;
        
        while(result != 1 && result != 4){
            result = isHappyNumber(result);
        }
        
        //Happy number always ends with 1
        if(result == 1)
            Console.WriteLine(num + " is a happy number");
        //Unhappy number ends in a cycle of repeating numbers which contains 4
        else if(result == 4)
            Console.WriteLine(num + " is not a happy number");  
    }
}

Output:

82 is a happy number

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//isHappyNumber() will determine whether a number is happy or not
function isHappyNumber($num){
    $rem = $sum = 0;
    
    //Calculates the sum of squares of digits
    while($num > 0){
        $rem = $num%10;
        $sum = $sum + ($rem*$rem);
        $num = intval($num/10);
    }
    return $sum;
}
 
$num = 82;
$result = $num;
 
while($result != 1 && $result != 4){
    $result = isHappyNumber($result);
}
 
//Happy number always ends with 1
if($result == 1)
    print($num . " is a happy number");
//Unhappy number ends in a cycle of repeating numbers which contains 4
else if($result == 4)
    print($num . " is not a happy number");   
?>
</body>
</html>

Output:

 82 is a happy number

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf