Back to: C#.NET Programs and Algorithms

**Happy Number Program in C# with Examples**

In this article, I am going to discuss the **Happy** **Number Program in C#** with Examples. Please read our previous article where we discussed the **Vampire Number Program in C#**. Here, in this article, first, we will learn what is a Happy Number and then we will see how to implement the Happy Number Program in C#. And finally, we will see how to print all the Happy Numbers between a range of numbers like between 1 to 100 or 100 to 1000, etc.

**What is a Happy Number?Â Â **

A happy number is a number that reaches 1 after a sequence of steps. In each step, the number is replaced by the sum of its squared digits.Â Â OrÂ Â

A number is called happy if it leads to 1 after a sequence of steps wherein each stepÂ number is replaced by the sum of squares of its digit that is if we start with Happy Number and keep replacing it with digits square sum, we reach 1.Â Â

**Example1: **

**Input: n = 19 **

**Output: True**

**Explanation**:

12 + 92 = 1 + 81 = 82

82 + 22 = 64 + 4 = 68

62 + 82 = 36 + 64 = 100

12 + 02 + 02 = 1 + 0 + 0 = 1

As we reached to 1, so, 19 is a Happy Number.’

**Example2: **

**Input: n = 20 **

**Output: False **

**Explanation: **

22 + 02 = 4 + 0 = 4, So 20 is not a happy number

First of all, we have to know what is Square number.Â Â Â Â

**SQUAREÂ NUMBER:**Â

A square number is the result when a number has been multiplied by itself. For example, 25 is a square number because it’s 5 x 5.Â Â

**SquareÂ Number Program in C#:**

The following program shows the Square Number Program in C#.Â Â

using System; public class SquareNumber { public static void Main() { Console.WriteLine("Enter a Number : "); int Number = Convert.ToInt16(Console.ReadLine()); double Sqr = Math.Pow(Number, 2); Console.WriteLine("Square of {0} is: {1}", Number, Sqr); Console.ReadLine(); } }

**Output**:Â Â

Â

**How to Implement Happy Number Program in C#?**

In order to implement the Happy Number Program in C#, we are going to use some operators and methods. We are going to use the very basic method to implement this program but before that, you should have some basic knowledge of the following operators.Â Â

**1) Modulus %:** Modulus Operator will calculate the remainder after an integer division.

**Example: 82 % 10 = 2 **

**Â Â Â Â Â Â Â Â Â 68 % 10 = 8 **

**2) Divides /:** Divides numerator by de-numerator

**Example: 82/ 10 = 8 **

**Â Â Â Â Â Â Â Â Â 68/ 10 = 6**

**Algorithm to Implement Happy Number:**

The idea is very simple, calculate the square of each digit present in the number and add it to a variable sum. If the resulting sum is equal to 1 then the given number is a happy number. Otherwise, it is not a happy number.Â Â

**Here are theÂ steps toÂ follow: –Â Â Â Â Â Â Â **

1) IsHappyNumber() determines whether a given number is a happy number or not.Â Â

- If the number is greater than 0, then calculate remainder rem by dividing the number by 10.Â Â
- Calculate the square of rem and add it to a variable sum.Â Â
- Divide number by 10.Â Â
- Repeat the steps from a to c till the sum of the square of all digits present in the number has been calculated.Â Â
- 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, the given number is a happy number.Â Â

6) If the result is equal to 4 then, the given number is not a happy number.Â

**Example: Happy Number Program in C#**

The following code shows how to implement the Happy Number program in 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() { Console.WriteLine("Enter a Number : "); int Number = Convert.ToInt16(Console.ReadLine()); int result = Number; while (result != 1 && result != 4) { result = IsHappyNumber(result); } //Happy number always ends with 1 if (result == 1) Console.WriteLine(Number + " is a happy number"); //Unhappy number ends in a cycle of repeating numbers which contains 4 else if (result == 4) Console.WriteLine(Number + " is not a happy number"); Console.ReadKey(); } }

**Output:**

Â

**C# Program to Print all Happy Numbers Between 1 and N**

The following C# Program will allow the user to input a number and then it will print all the Happy numbers between 1 and that input number.

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() { Console.WriteLine("Enter a Number : "); int Number = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Happy Numbers Between 1 and " + Number + " : "); for (int i = 1; i<= Number; i++) { int num = i; int result = num; while (result != 1 && result != 4) { result = IsHappyNumber(result); } //Happy number always ends with 1 if (result == 1) { Console.Write(num + " "); } } Console.ReadKey(); } }

**Output:**

In the next article, I am going to discuss **How to Implement the Automorphic** **Number Program in C#** with Examples. Here, in this article, I try to explain **How to Implement the Happy** **Number Program in C#**Â with Examples and I hope you enjoy this Happy Number Program in C# article.

**About the Author: Pranaya Rout**

Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.