Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Check If a Number is a Happy Number
We are given a number as input, and we need to check whether it is a happy number or not. In this article, we will learn how to check if a number is a happy number using C#.
What is a Happy Number?
A happy number is a number that eventually becomes equal to 1 when we repeatedly replace it with the sum of the squares of its digits. If the number gets stuck in a cycle and never reaches 1, then it is not a happy number.
Example 1
- Input: 19
- Output: True
Explanation: 19 is a happy number because repeatedly calculating the sum of squares of its digits eventually leads to 1
Step 1: 1² + 9² = 1 + 81 = 82
Step 2: 8² + 2² = 64 + 4 = 68
Step 3: 6² + 8² = 36 + 64 = 100
Step 4: 1² + 0² + 0² = 1 + 0 + 0 = 1
Example 2
- Input: 20
- Output: False
Explanation: 20 is not a happy number because it gets trapped in a cycle and never reaches 1
Step 1: 2² + 0² = 4 + 0 = 4
Step 2: 4² = 16
Step 3: 1² + 6² = 1 + 36 = 37
Step 4: 3² + 7² = 9 + 49 = 58
Step 5: 5² + 8² = 25 + 64 = 89
Step 6: 8² + 9² = 64 + 81 = 145
Step 7: 1² + 4² + 5² = 1 + 16 + 25 = 42
Step 8: 4² + 2² = 16 + 4 = 20
We get back to 20, forming a cycle. Since we never reach 1, the number 20 is not happy.
Algorithm
We use a HashSet to track sums we have encountered to detect cycles. The algorithm calculates the sum of squares of digits repeatedly until either reaching 1 (happy number) or detecting a cycle (not happy).
Steps for Implementation
- Create a function that calculates the sum of squares of digits for a given number.
- Use a HashSet to store previously seen sums to detect cycles.
- If the sum equals 1, the number is happy.
- If the sum is already in the HashSet, the number is not happy (cycle detected).
- Repeat the process until we reach 1 or detect a cycle.
C# Program to Check if a Number is a Happy Number
using System;
using System.Collections.Generic;
class HappyNumberChecker {
// Calculates the sum of the squares of digits of the given number
static int GetSumOfSquares(int number) {
int sum = 0;
while (number > 0) {
int digit = number % 10;
sum += digit * digit;
number /= 10;
}
return sum;
}
// Checks if the given number is a Happy Number
public static bool IsHappyNumber(int number) {
HashSet<int> seenSums = new HashSet<int>();
while (number != 1 && !seenSums.Contains(number)) {
seenSums.Add(number);
number = GetSumOfSquares(number);
}
return number == 1;
}
static void Main() {
int[] testNumbers = {19, 20, 7, 10};
foreach (int num in testNumbers) {
if (IsHappyNumber(num)) {
Console.WriteLine($"{num} is a Happy Number.");
} else {
Console.WriteLine($"{num} is not a Happy Number.");
}
}
}
}
The output of the above code is
19 is a Happy Number. 20 is not a Happy Number. 7 is a Happy Number. 10 is not a Happy Number.
Time and Space Complexity
- Time Complexity: O(log n × m), where calculating sum of squares takes O(log n) time and m is the number of iterations before detecting 1 or a cycle.
- Space Complexity: O(m), where m is the maximum number of unique sums stored in the HashSet before reaching 1 or detecting a cycle.
Conclusion
A happy number is determined by repeatedly replacing it with the sum of squares of its digits until either reaching 1 (happy) or detecting a cycle (not happy). Using a HashSet to track previously seen sums efficiently detects cycles and determines if a number is happy.
