Happy Number in C
What is Happy Number in C?
A positive integer that is defined as cheerful in mathematics. Replace any positive integer by the sum of its squares, then carry out this procedure repeatedly until the value equals 1 (signifying happiness) or loops indefinitely without reaching 1 (signifying unhappiness). A number is regarded as happy if the process concludes in 1.
What corresponds to the happy number equation?
Unlike an equation with variables and constants, the equation of a happy number is not expressed by a specific mathematical formula. Instead, it's a method or algorithm that works by repeatedly iterating through a number's digits and running various calculations to ascertain whether or not the number is content.
The general method to ascertain whether a number is content is as follows:
- Count up from one when you begin.
- Substitute the number with the sum of its digits' squares.
- With the new number, repeat step 2.
- Continue doing this until the number either reaches 1 (making you happy) or enters a cycle that has already happened (making you upset).
As an illustration,
consider the number 19:
19 -> 1^2 + 9^2 = 1 + 81 = 82
82 -> 8^2 + 2^2 = 64 + 4 = 68
68 -> 6^2 + 8^2 = 36 + 64 = 100
100 -> 1^2 + 0^2 + 0^2 = 1
19 is a joyful number since the procedure ends with a 1.
The secret is to monitor the numbers produced throughout the procedure and look for repetitions. If you run into a number that has been seen before, it indicates that the number is unhappy and that you are in a loop.
Since iteration and checking for certain requirements are required to make the determination, there is no straightforward algebraic formula that specifies happy numbers.
Algorithm
Yes, here is the formula to determine whether a number is a happy number:
- Set the supplied number n to zero.
- While n (the known cycle value) is not equal to 1 or 4, respectively:
- Set a variable's sum value to 0.
- As long as n exceeds 0:
- Take n% 10 to find n's final digit.
- The final digit's square should be added to the total.
- By dividing the integer n by 10, update n.
- Put n equal to the sum value.
- The original number is a cheerful number if n equals 1.
- The original number is not a happy number if n = 4, thus.
The method repeatedly replaces the value with the sum of its squares until it either reaches 1 (which makes the user happy) or begins a loop that terminates at 4 (which makes the user sad).
Flow Chart
![Happy Number in C](https://static.tutorialandexample.com/c/happy-number-in-c1.png)
Program
#include <stdio.h>
int isHappy(int n) {
while (n != 1 && n != 4) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
n = sum;
}
return n == 1;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isHappy(num)) {
printf("%d is a happy number.\n", num);
} else {
printf("%d is not a happy number.\n", num);
}
return 0;
}
Output
![Happy Number in C](https://static.tutorialandexample.com/c/happy-number-in-c2.png)
Explanation
Description of the isHappy function:
- The parameter for the function is an integer n.
- It then enters a while loop that runs as long as n does not equal 1 (which denotes happiness) or 4, which denotes a known cycle of sadness.
- A variable named total is given a 0 initial value inside the loop. The squared sum of the digits will be kept in this variable.
- The square sum of n's digits is calculated using a nested while loop. It calculates the final digit repeatedly, squares it, and then adds it to the total. Finally, it subtracts the final digit from n by dividing it by 10.
- The value of n is updated with the calculated sum, and the loop resumes after computing the sum of the squares of digits for the current n.
- If n is equal to 1 after the loop ends, the method returns 1 (signifying the number is content), otherwise it returns 0.
The primary function takes a user-supplied number and uses the isHappy function to determine whether or not it is a happy number. According on the outcome, it publishes the relevant message.
Complexity Analysis
In a nutshell, the following summarises the time complexity of the method to detect whether a given number is a happy number when using the suggested approach:
- Best Case: O(1) (for positive integers)
- Average Case: O(d), where 'd' is the number of digits in the input number.
- The worst-case scenario: O(cycle_length * d), where "cycle_length" refers to the length of the cycle that the number enters.
Due to the fact that most numbers have relatively short cycle durations, the method operates admirably in the majority of practical situations, with the worst-case scenario being uncommon in most cases.