forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeonNumber.dart
48 lines (41 loc) · 862 Bytes
/
NeonNumber.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
/*
A number is said to be a neon number when the sum of digits of the square of the number
is equal to the number itself
*/
import 'dart:io';
// Function to check if number is a neon number
bool isNeon(int num) {
int sum = 0;
int square = num*num;
while (square > 0) {
int digit = square % 10;
sum += digit;
square ~/= 10;
}
if (sum == num) {
return true;
}
return false;
}
// Main Function with driver code
void main() {
print("Enter a number :");
int num = int.parse(stdin.readLineSync()!);
// Call function to check if number is a neon number
if (isNeon(num)) {
print("$num is a Neon Number");
} else {
print("$num is not a Neon Number");
}
}
/**
Space Complexity O(1)
Time Complexity O(log(n))
Sample input/output:
Enter a number :
9
9 is a Neon Number
Enter a number :
12
12 is not a Neon Number
*/