In this tutorial, we will write a C program to check if the number is happy number or not. Let us start by defining a happy number.

## Happy Number

A number is said to be a **happy number** if it yields 1 after a few steps and each step is the sum of the squares of the digits of its results. The sum of the squares starts with the given number and till it reaches one.

Follow the diagram below:

**Explanation**: In the above diagram input is **19**. Then the digits 1 and 9 are squared which gives the result **82**. After that, the digits 8 and 2 are squared and yield the results **68** and this continues till the final result is 1. And then we can say that 19 is a **happy number**, if not then it is not a happy number.

**Happy Number Program in C**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | #include <stdio.h> int happyNumber(int); int main() { int num = 82; printf("Enter the number: "); scanf("%d", &num); int temp = num; while (temp != 1 && temp != 4) { temp = happyNumber(temp); } //check for 1 if (temp == 1) printf("%d is a happy number", num); else if (temp == 4) printf("%d is NOT a happy number", num); return 0; } //user defined function int happyNumber(int num) { int rem = 0, sum = 0; //Calculation while (num > 0) { rem = num % 10; sum = sum + (rem *rem); num /= 10; } return sum; } |

**Output:**

`Enter the number: 32`

32 is a happy number