## What is Happy Numbers?

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 the square of each digit of that 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

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

## Code of Calculate (Check) Happy Number?

### Write a C program to check given number is Happy Number or 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 Sad Number.

``````#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;
}
``````

Output :

Enter the number : 82
82 is Happy Number

## Algorithm of Check Happy Number

Step 1: INITILIZED ‘n’>

Step 2: READ INPUT n <- FROM USER

Step 3: SET temp = n AND sum = 0 AND INITILIZED digits

Step 4: REPEATE Step 5 while(sum != 1 && sum != 4)

Step 5: RESET sum = 0, AND REPEATE Step 6, Step 7, Step 8 while(n>0)

Step 6: SET digits = n % 10

Step 7: SET sum = sum + (digits * digits)

[End loop]

Step 9: n = sum

[End Loop]

Step 10: CHECK sum=1 PRINT “GIVEN NUMBER IS HAPPY NUMBER” otherwise go to step 11

Step 11: PRINT “GIVEN NUMBER IS NOT A HAPPY NUMBER”

Step 12: Exit

## 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;
}
``````

Output:

Enter the Limit : 100
1 7 10 13 19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100

## Algorithm to print all Happy Numbers

Step 1: INITILIZED VARIABLE “limit”

Step 2: READ INPUT limit <- FROM USER

Step 3: INITILIZED “n” and SET “sum = 0” AND INITLIZED “digits”

Step 4: REPEAT Step 5 , Step 6, Step 12 for(int i=1; i<=limit; i++), when Condition not False

Step 5: SET n = i AND RESET sum = 0

Step 6: REPEATE Step 7 while(sum != 1 && sum != 4)

Step 7: RESET sum = 0, AND REPEATE Step 8, Step 9, Step 10 while(n>0)

Step 8: SET digits = n % 10

Step 9: SET sum = sum + (digits * digits)

Step 10: SET n = n / 10

[End loop]

Step 11: n = sum

[End Loop]

Step 12: CHECK sum=1 PRINT “i”

[End Loop]

Step 13: Exit

## Program to print the all Unhappy (Sad) 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 Unhappy (Sad) 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;
}
``````

Output:

Enter the Limit : 100
2 3 4 5 6 8 9 11 12 14 15 16 17 18 20 21 22 24 25 26 27 29 30 33 34 35 36 37 38 39 40 41 42 43 45 46 47 48 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 69 71 72 73 74 75 76 77 78 80 81 83 84 85 87 88 89 90 92 93 95 96 98 99

## Algorithm to print all Unhappy Numbers

Step 1: INITILIZED VARIABLE “limit”

Step 2: READ INPUT limit <- FROM USER

Step 3: INITILIZED “n” and SET “sum = 0” AND INITLIZED “digits”

Step 4: REPEAT Step 5 , Step 6, Step 12 for(int i=1; i<=limit; i++), when Condition not False

Step 5: SET n = i AND RESET sum = 0

Step 6: REPEATE Step 7 while(sum != 1 && sum != 4)

Step 7: RESET sum = 0, AND REPEATE Step 8, Step 9, Step 10 while(n>0)

Step 8: SET digits = n % 10

Step 9: SET sum = sum + (digits * digits)

Step 10: SET n = n / 10

[End loop]

Step 11: n = sum

[End Loop]

Step 12: CHECK sum!=1 PRINT “i”

[End Loop]

Step 13: Exit

...

## Sunny Number in programming

A number is a sunny number. If 1 is added to that number and the square root of it becomes a whole number.

## Program to check whether number is Disarium Number

A Number is a disarium number if the sum of the digits powered with their respective positions is equal to the number itself.

## Floyd Triangle | Programs | Algorithm

A Floyd Triangle is right angled triangle consisting of consecutive natural numbers starting with one and then going down through the rows.

## Write a program to reverse a number

Write a program to reverse a number. What is reverse number?

## Sum of digit calculate program

We can get the sum of the digit by adding every digits of the given number forgetting the place of value of the digit.

## Program to Calculate the power of given numbers by user in c and cpp

In this program, we will take two inputs from the users, one will be the base and the another one is exponent.