You're online now.

Hurray! you are online now.

Happy Number | with Example and Programs

Replace the number by the sum of the squares of its digits, and repeat the process. At the end, if the number is equals to 1 then it is a Happy Number, or it loops endlessly in a cycle that does not include 1 (if it is not a happy number then this process will end at 4).

For Example:
31 is a Happy Number.
31 = 32 + 12 = 9 + 1 = 10
10: 12 + 02 = 1 + 0 = 1
As we reached to 1, 31 is a Happy Number.

How do I know my happy number?

The biggest question is how to we know whether the given number is a Happy Number or not. For that we have to do that the number we have been given, we have to calculate square of each digit of the number, and add the square to a variable, if the sum of the square is 1 then the given number is a happy number.

Why 4 is not a happy number (Unhappy Number)

4 is not a happy number, because if we start with 4 to calculate the happy number,
4: 42 = 16
16: 12 + 62 = 1 + 36 = 37

37: 32 + 72 = 9 + 49 = 58
58: 52 + 82 = 25 + 64 = 89

89: 82 + 92 = 64 + 81 = 145
145: 12 + 42 + 52 = 1 + 16 + 25 = 42
42: 42 + 22 = 16 + 4 = 20
20: 22 + 02 = 4 + 0 = 4

If we calculate this, it will come back to the same number, just we started with 4 and it turned 4. It forms a cycle so 4 is not Unhappy Number. And there 1 is happy number because if we square 1 we get only 1.

Program to check given number is Happy Number or not

// Write a C program to check given number is happy number of not.
    #include<stdio.h>
    int main(){
        int n;
        printf("Enter the number : ");
        scanf("%d", &n);
        int temp = n, digits;
        int sum = 0;
        while(sum!=1 && sum!=4){
            sum = 0;
            while(n>0){
                digits = n % 10;
                sum = sum + (digits*digits);
                n = n / 10;
            }
            n = sum;
        }
        if(sum == 1){
            printf("%d is Happy Number", temp);
        }else{
            printf("%d is Not a Happy Number", temp);
        }
        return 0;
    }
    
// Write a c++ program to check given number is happy number or not.
    #include<iostream>
    using namespace std;
    
    int main(){
        int n;
        cout<<"Enter the number : ";
        cin>>n;
        int temp = n, digits;
        int sum = 0;
        while(sum!=1 && sum!=4){
            sum = 0;
            while(n>0){
                digits = n % 10;
                sum = sum + (digits*digits);
                n = n / 10;
            }
            n = sum;
        }
        if(sum == 1){
            cout<<temp<<" is Happy Number";
        }else{
            cout<<temp<<" is Not a Happy Number";
        }
        return 0;
    }
    

Algorithm to check happy number

  1. Initialized ‘n’
  2. READ input n, from user
  3. SET temp = n and sum = 0 and Initialized digits
  4. REPEATE step 5 while(sum ≠ 1 && sum ≠ 4)
  5. RESET sum = 0, and REPEATE step 6, step 7, step 8 while(n > 0)
  6. SET digits = n % 10
  7. SET sum = sum + (digits * digits)
    [END LOOP]
  8. n = sum
    [END LOOP]
  9. Check sum == 1 PRINT “Given number is happy number” otherwise
  10. PRINT “Given number is not a happy number”
  11. EXIT

Print the all happy number between 1 to till n

// Program to print the all Happy number between 1 to till n in c programming language.
    #include<stdio.h>
    int main(){
        int limit;
        printf("Enter the Limit : ");
        scanf("%d", &limit);
        int n, digits, sum = 0;
        for(int i=1; i<=limit; i++){
            n = i;
            sum = 0;
            while(sum!=1 && sum!=4){
                sum = 0;
                while(n>0){
                    digits = n % 10;
                    sum = sum + (digits*digits);
                    n = n / 10;
                }
                n = sum;
            }
            if(sum == 1){
                printf("%d ", i);
            }
        }
        return 0;
    }
    
// Program to print the all Happy numbers between 1 to till n in c++ programming language.
    #include<iostream>
    using namespace std;
    int main(){
        int limit;
        cout<<"Enter the Limit : ";
        cin>>limit;
        
        int n, digits;
        int sum = 0;
    
        for(int i=1; i<=limit; i++){
            n = i;
            sum = 0;
            while(sum!=1 && sum!=4){
                sum = 0;
                while(n>0){
                    digits = n % 10;
                    sum = sum + (digits*digits);
                    n = n / 10;
                }
    
                n = sum;
            }
    
            if(sum == 1){
                cout<<i<<" ";
            }
        }
    
        return 0;
    }
    

Algorithm to print all happy numbers

  1. Initialized variable “limit”
  2. READ input limit, from user
  3. Initialized ‘n’ and set ‘sum = 0’ and initialized ‘digits’
  4. REPEAT step 5, step 6, step 12 for(int i = 1; i ≤limit; i++), when condition not false
  5. SET n = i AND RESET sum = 0
  6. REPEAT Step 7 while (sum ≠ 1 && sum ≠ 4)
  7. RESET sum = 0, AND REPEAT step 8, step 9, step 10 while (n > 0)
  8. SET digits = n % 10
  9. SET sum = sum + (digits * digits)
  10. SET n = n / 10
    [END LOOP]
  11. Step 11: n = sum
    [END LOOP]
  12. Check sum = 1 PRINT ‘i’
    [END LOOP]
  13. EXIT

Print all Unhappy(Sad) number between 1 to till n

// C Program to print the Unhappy number between 1 to till n.
    #include<stdio.h>
    int main(){
        int limit;
        printf("Enter the Limit : ");
        scanf("%d", &limit);
        int n, digits, sum = 0;
        for(int i=1; i<=limit; i++){
            n = i;
            sum = 0;
            while(sum!=1 && sum!=4){
                sum = 0;
                while(n>0){
                    digits = n % 10;
                    sum = sum + (digits*digits);
                    n = n / 10;
                }
                n = sum;
            }
            if(sum != 1){
                printf("%d ", i);
            }
        }
        return 0;
    }
    
// C++ Program to print all Unhappy number between 1 to till n.
    #include<iostream>
    using namespace std;
    int main(){
        int limit;
        cout<<"Enter the Limit : ";
        cin>>limit;
        
        int n, digits;
        int sum = 0;
    
        for(int i=1; i<=limit; i++){
            n = i;
            sum = 0;
            while(sum!=1 && sum!=4){
                sum = 0;
                while(n>0){
                    digits = n % 10;
                    sum = sum + (digits*digits);
                    n = n / 10;
                }
    
                n = sum;
            }
    
            if(sum != 1){
                cout<<i<<" ";
            }
        }
    
        return 0;
    }
    
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...