
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Check If a Number is a Happy Number
Problem Description
We are given a number as input, we have to check whether the number is a happy number or not. In this article, we are going to learn how we can check if a number is a happy number or not in C#.What is a happy number?
A happy number is a special type of number that will at last become equal to 1 when we replace the number with the sum of the square of its digits again and again. If the number is stuck in the loop such that it never becomes equal to 1 then the number is not a happy number.Example 1
- Input: 19
-
Output: True
Explanation
Here, 19 is a happy number because if we calculate the sum of the square of 19 again and again, it will reach a point, where it will give the result 1.
Sum 1: 12 + 92 = 1 + 81 = 82 , Now take 82
Sum 2: 82 + 22 = 64 + 4 = 68, Take 68
Sum 3: 62 + 82 = 36 + 64 = 100, Now take 100
Sum 4: 12 + 02 + 02 = 1 + 0 + 0 = 1
Example 2
-
Input: 20
- Output: False
Explanation
Here, 20 is not a happy number because if we calculate the sum of the square of 20 again and again, it will never reach to 1, here it will form a never-ending cycle.
Now, let's check why 20 is not a happy number
Sum 1: 22 + 02 = 4 + 0 = 4, Now take 4.
Sum 2: 42 =16, Now, take 16
Sum 3: 12 + 62 = 1 + 36 = 37, Now, take 37
Sum 4: 32 + 72 = 9 + 49 = 58, Now, take 58
Sum 5: 52 + 82 = 25 + 64 = 89, Take 89
Sum 6: 82 + 92 = 64 + 81 = 145, Take 145
Sum 7: 12 + 42 + 52 = 1 + 16 + 25 = 42, Now, take 42
Sum 8: 42 + 22 = 16 + 4 = 20.
We again get 20, so it forms a cycle. Since we never reach 1, so 20 is not a happy number.
Checking If a Number is a Happy Number
We use a HashSet to store the track of sums we have encountered. With the help of a function, we calculate the sum of the squares of the digits of a number. If the sum of digits is equal to 1, the number is a happy number. If the sum is present in the HashSet, then it is part of a cycle, so the number is not happy. Otherwise, we add the sum to the HashSet and repeat until we find the sum again present in the hashmap.Steps for Implementation
- We create a function that calculates the sum of squares of digits for a given number.
- No, we use a hashset to store sums and if the sum repeats, it indicates a cycle.
- If the sum is equal to 1, the number is a happy number.
- If the sum is already found in the hashset, the number is not a happy number.
- We repeat the process until the number is equal to 1 i.e. happy number or a cycle is detected i.e. not a happy number.
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) { // Using HashSet<int> for type safety 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 number = 19; if (IsHappyNumber(number)) { // Corrected string interpolation to remove extra space Console.WriteLine($"{number} is a Happy Number."); } else { // Corrected string interpolation to remove extra space Console.WriteLine($"{number} is not a Happy Number."); } } }
Output
19 is a Happy Number.Time Complexity: O(log n à m), calculating sum of squares takes O(logn) time and m is the number of iterations before detecting 1 or a cycle.
Space Complexity: O(m)