C Topics
- C Intro
- C Control Statement
- C Control Loops
- C String Functions
- C Math Functions
- C Header Files
- C Interview Programs
- Abundant Number
- Amicable Number
- Armstrong Number
- Average of N Numbers
- Automorphic Number
- Biggest of three numbers
- Binary to Decimal
- Common Divisors
- Composite Number
- Condense a Number
- Cube Number
- Decimal to Binary
- Decimal to Octal
- Disarium Number
- Even Number
- Evil Number
- Factorial of a Number
- Fibonacci Series
- GCD
- Happy Number
- Harshad Number
- LCM
- Leap Year
- Magic Number
- Matrix Addition
- Matrix Division
- Matrix Multiplication
- Matrix Subtraction
- Matrix Transpose
- Maximum Value of an Array
- Minimum Value of an Array
- Multiplication Table
- Natural Number
- Number Combination
- Odd Number
- Palindrome Number
- Pascalβs Triangle
- Power of 2
- Power of 3
- Pronic Number
- Perfect Number
- Perfect Square
- Prime Factor
- Prime Number
- Smith Number
- Strong Number
- Sum of Array
- Sum of Digits
- Swap Two Numbers
- Triangular Number
- C Star Pattern
- C Number Pattern
- C Alphabet Pattern
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."
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.
#include <stdio.h>
// Function to calculate the sum of squares of digits
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
int isHappyNumber(int n) {
int slow = n, fast = n;
do {
slow = sumOfSquares(slow);
fast = sumOfSquares(sumOfSquares(fast));
} while (slow != fast);
return (slow == 1);
}
// Driver program
int main() {
// Replace this value with your desired number
int number = 19;
// Check if the number is a happy number
if (isHappyNumber(number)) {
printf("%d is a Happy Number.\n", number);
} else {
printf("%d is not a Happy Number.\n", number);
}
return 0;
}
π» Testing the Program
To test the program with a different number, replace the value of number in the main function.
19 is a Happy Number.
Compile and run the program to check if the number is a happy number.
π§ How the Program Works
- The program defines a function sumOfSquares to calculate the sum of the squares of digits of a given number.
- It then defines a function isHappyNumber that checks whether a given number is a happy number using Floyd's cycle detection algorithm.
- The main function 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.
#include <stdio.h>
// Function to calculate the sum of the squares of digits
int sumOfSquares(int num) {
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += digit * digit;
num /= 10;
}
return sum;
}
// Function to check if a number is happy
int isHappy(int num) {
int slow = num, fast = num;
do {
slow = sumOfSquares(slow);
fast = sumOfSquares(sumOfSquares(fast));
} while (slow != fast);
return (slow == 1);
}
// Driver program
int main() {
printf("Happy numbers in the range 1 to 50:\n");
for (int i = 1; i <= 50; ++i) {
if (isHappy(i)) {
printf("%d ", i);
}
}
return 0;
}
π» Testing the Program
Happy numbers in the range 1 to 50: 1 7 10 13 19 23 28 31 32 44 49
Compile and run the program to see the happy numbers in the range from 1 to 50.
π§ How the Program Works
- The program defines a function sumOfSquares to calculate the sum of the squares of digits in a number.
- The function isHappy checks if a number is a happy number by using Floyd's Cycle Detection Algorithm.
- The main function iterates through numbers from 1 to 50 and prints the happy numbers in the specified range.
π§ 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.