C# Basic
C# Interview Programs
- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number
C# Program to Check Happy Number
Photo Credit to CodeToFun
π Introduction
In the domain of programming, certain mathematical properties and patterns are often explored. One such interesting concept is the notion of "happy numbers."
In this tutorial, we will delve into a C# program designed to check whether a given number is a happy number.
π Example
Let's take a look at the C# code that checks whether a given number is a happy number or not.
using System;
class Program {
// Function to calculate the sum of squares of digits
static int SumOfSquares(int n) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
return sum;
}
// Function to check if a number is a happy number
static bool IsHappyNumber(int n) {
int slow = n, fast = n;
do {
slow = SumOfSquares(slow);
fast = SumOfSquares(SumOfSquares(fast));
} while (slow != fast);
return (slow == 1);
}
// Driver program
static void Main() {
// Replace this value with your desired number
int number = 19;
// Check if the number is a happy number
if (IsHappyNumber(number)) {
Console.WriteLine($"{number} is a Happy Number.");
} else {
Console.WriteLine($"{number} is not a Happy Number.");
}
}
}
π» Testing the Program
To test the program with a different number, replace the value of number in the Main method.
19 is a Happy Number.
π§ How the Program Works
- The program defines a class Program containing static methods SumOfSquares and IsHappyNumber.
- The SumOfSquares method calculates the sum of the squares of digits of a given number.
- The IsHappyNumber method checks whether a given number is a happy number using Floyd's cycle detection algorithm.
- The Main method tests the program with a sample number (in this case, 19).
π Between the Given Range
Let's take a look at the C# code that checks for Happy Numbers in the specified range.
using System;
using System.Collections.Generic;
class Program {
// Function to check if a number is happy
static bool IsHappy(int num) {
HashSet < int > seen = new HashSet < int > ();
while (num != 1 && !seen.Contains(num)) {
seen.Add(num);
num = GetNextNumber(num);
}
return num == 1;
}
// Function to get the next number in the iteration
static int GetNextNumber(int num) {
int result = 0;
while (num > 0) {
int digit = num % 10;
result += digit * digit;
num /= 10;
}
return result;
}
// Driver program
static void Main() {
Console.WriteLine("Happy numbers in the range 1 to 50:");
for (int i = 1; i <= 50; i++) {
if (IsHappy(i)) {
Console.Write($"{i} ");
}
}
Console.WriteLine();
}
}
π» Testing the Program
Happy numbers in the range 1 to 50: 1 7 10 13 19 23 28 31 32 44 49
Simply run the program to see the happy numbers in the specified range.
π§ How the Program Works
- The program defines a function IsHappy that checks whether a number is a happy number or not.
- Inside the IsHappy function, it uses a HashSet to keep track of numbers encountered in the iteration.
- The GetNextNumber function calculates the next number in the iteration.
- The Main function iterates through numbers from 1 to 50 and prints the happy numbers.
π§ Understanding the Concept of Happy Number
A happy number is a positive integer defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1, or it loops endlessly in a cycle that does not include 1.
For example, let's take the number 19:
- 12+92 = 82
- 82+22 = 68
- 62+82 = 100
- 12+02+02 = 1
In this case, 19 is a happy number because the process ends with 1.
π’ Optimizing the Program
While the provided program is effective, there are various ways to optimize and enhance it further. Consider exploring different algorithms or incorporating additional features based on your specific requirements.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C# Program to Check Happy Number), please comment here. I will help you immediately.