-
Notifications
You must be signed in to change notification settings - Fork 1k
/
HarshadNumber.dart
52 lines (43 loc) · 995 Bytes
/
HarshadNumber.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
/*
A number is said to be a Harshad Number if it is divisible by the sum of its digits.
For example: The number 84 is divisible by the sum (12) of its digits (8, 4).
*/
import 'dart:io';
// Check if a number is a Harshad number
bool isHarshad(int number) {
int sum = 0;
int copy = number;
// Seperate individual digits and add to sum
while (number > 0) {
int digit = number % 10;
number ~/= 10;
sum += digit;
}
// Check if number is divisible by sum
if (copy % sum == 0) {
return true;
}
return false;
}
// Main Function, with driver code
void main() {
print("Enter a number:");
int input = int.parse(stdin.readLineSync()!);
// Call isHarshad function
if (isHarshad(input)) {
print("$input is a Harshad Number");
} else {
print("$input is not a Harshad Number");
}
}
/**
Time Complexity: O(log(n))
Space Complexity: O(1)
Sample Input/Output
Enter a number:
18
18 is a Harshad Number
Enter a number:
123
Sum of digits in 123 is 6
*/