-
Notifications
You must be signed in to change notification settings - Fork 1k
/
HappyNumber.dart
56 lines (46 loc) · 1.05 KB
/
HappyNumber.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
A happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit
until the sum becomes a single digit number.
*/
import 'dart:io';
// Function to check if number is a Happy Number
bool isHappyNumber(int num) {
int sum = num;
// do while loop used as square of digits need to be taken a minimum of one time
do{
int copy = sum;
sum = 0;
// calculating sum of square of individual digits
while(copy > 0){
int digit = copy % 10;
sum += (digit*digit);
copy ~/= 10;
}
}
while(sum > 9);
if(sum == 1){
return true;
}
return false;
}
void main() {
print("Enter a number :");
int num = int.parse(stdin.readLineSync()!);
// Call function to check if number is a Happy number
if (isHappyNumber(num)) {
print("$num is a Happy Number");
} else {
print("$num is not a Happy Number");
}
}
/**
Space Complexity O(1)
Time Complexity O(nlog(n))
Sample input/output:
Enter a number :
19
19 is a Happy Number
Enter a number :
20
20 is not a Happy Number
*/