forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArmstrong.dart
46 lines (40 loc) · 901 Bytes
/
Armstrong.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
import 'dart:math';
import 'dart:io';
// checks if num is armstrong or not
bool isArmstrong(int num) {
bool isArmNum = false;
int result = 0;
int temp = num;
// Count No Digits in numbers
int numberOfDigits = temp.toString().length;
while (temp > 0) {
int remainder = temp % 10;
result += pow(remainder, numberOfDigits).toInt();
temp ~/= 10;
}
if (result == num) {
isArmNum = true;
}
return isArmNum;
}
// Main Function, Entry Point of Program
void main() {
print("Enter a number:");
int num = int.parse(stdin.readLineSync()!);
// Call function to check number is Armstrong
if (isArmstrong(num)) {
print("$num is an Armstrong Number");
} else {
print("$num is not an Armstrong Number");
}
}
/**
* Sample input/output:
* Enter Number:
* 153
* 153 is an Armstrong Number
*
* Enter Number:
* 224
* 224 is not an Armstrong Number
*/