---
title: "Happy Number"  
description: "Happy Number"  
author: "Steilla Mitchel"  
published: 2024-06-12  
updated: 2024-06-12  
canonical: https://www.mindstick.com/forum/160729/happy-number  
category: "c#"  
tags: ["c#", "programming language", "programming help", "programs"]  
reading_time: 2 minutes  

---

# Happy Number

Write an [algorithm](https://www.mindstick.com/blog/119/implementing-cryptography-in-c-sharp-dot-net-by-using-sha1-algorithm) to [determine if](https://www.mindstick.com/forum/158797/create-a-rust-program-to-determine-if-a-number-is-prime-or-not) a number `n` is happy.

A **happy number** is a number defined by the following [process](https://yourviews.mindstick.com/story/1525/7-important-factors-that-may-affect-the-learning-process):

- [Starting](https://www.mindstick.com/blog/12024/things-you-need-to-know-when-starting-your-own-business) with any positive [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer), [replace](https://yourviews.mindstick.com/view/81330/cow-antibody-research-in-usa-a-step-ahead-to-replace-plasma-therapy) the number by the sum of the squares of its digits.
- [Repeat](https://www.mindstick.com/forum/156775/give-an-example-of-the-linq-repeat-method) the process until the number [equals](https://www.mindstick.com/interview/33895/difference-between-and-equals-method-in-c-sharp) 1 (where it will stay), or it **[loops](https://www.mindstick.com/forum/161927/explain-the-python-while-loops-with-example) endlessly in a [cycle](https://yourviews.mindstick.com/view/81850/possible-effects-of-solar-weather-cycle)** which does not include 1.
- Those numbers for which this process **ends in 1** are happy.

Return `true` *if* `n` *is a happy number, and* `false` *if not*.

## Replies

### Reply by Ravi Vishwakarma

Let's write code to check whether a number is `IsHappy` or not happy in C#.

```cs
// Method to determine if a number is a happy number
public static bool IsHappy(int n)
{
    // Continue the process while n is greater than 10
    // If n becomes less than or equal to 10, we exit the loop to avoid infinite loops for unhappy numbers
    while (n > 10)
    {
        // Initialize the result with the initial number
        // Reset result to 0 to calculate the sum of the squares of the digits
        int result = 0;

        // Continue extracting and squaring digits until n becomes 0
        while (n != 0)
        {
            // Get the last digit of n
            int LastDigit = n % 10;

            // Add the square of the last digit to result
            result += (LastDigit * LastDigit);

            // Remove the last digit from n
            n /= 10;
        }

        // Update n to the result to repeat the process with the new number
        n = result;
    }

    // If the final value of n is 1, return true (indicating the number is happy)
    // Otherwise, return false (indicating the number is not happy)
    return n == 1;
}
```

Now call this function from the main function in the Program class.

```cs
public static void Main()
{
    IList<int> list = new List<int>
    {
        19, 1, 6, 100, 45
    };

    foreach (var item in list)
    {
        Console.WriteLine(item + " " + Program.IsHappy(item));
    }

    Console.ReadLine();
}
```

## Output :

```cs
19 True
1 True
6 False
100 True
45 False
```


---

Original Source: https://www.mindstick.com/forum/160729/happy-number

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
