Armstrong Number Programme in Dart

 import 'dart:io';

import 'dart:math';

void main() {
print("Enter any number to check for Armstrong number : ");
var num = int.parse(stdin.readLineSync()!);
int temp = num;
int lengthDigits = 0;

/// Finding the length of number given
while (temp > 0) {
temp = temp ~/ 10;
lengthDigits++;
}
print("Length of Digits is : $lengthDigits");

temp = num;
double sum = 0;

/// Getting the sum of digit's power
while (temp > 0) {
int rem = temp % 10;
sum = sum + pow(rem, lengthDigits);
temp = temp ~/ 10;
}
print("Sum is : $sum");

if (sum == num) {
print("The $num is an Armstrong number.");
} else {
print("The $num is not an Armstrong number.");
}
}
Enter any number to check for Armstrong number : 
173
Length of Digits is : 3
Sum is : 371.0
The 173 is not an Armstrong number.
Enter any number to check for Armstrong number : 
153
Length of Digits is : 3
Sum is : 153.0
The 153 is an Armstrong number.
Enter any number to check for Armstrong number : 
140
Length of Digits is : 3
Sum is : 65.0
The 140 is not an Armstrong number.
Enter any number to check for Armstrong number : 
370
Length of Digits is : 3
Sum is : 370.0
The 370 is an Armstrong number.
Enter any number to check for Armstrong number : 
368
Length of Digits is : 3
Sum is : 755.0
The 368 is not an Armstrong number.
Enter any number to check for Armstrong number : 
407
Length of Digits is : 3
Sum is : 407.0
The 407 is an Armstrong number.
Enter any number to check for Armstrong number : 
1634
Length of Digits is : 4
Sum is : 1634.0
The 1634 is an Armstrong number.
Enter any number to check for Armstrong number : 
5742
Length of Digits is : 4
Sum is : 3298.0
The 5742 is not an Armstrong number.
Enter any number to check for Armstrong number : 
8208
Length of Digits is : 4
Sum is : 8208.0
The 8208 is an Armstrong number.
Enter any number to check for Armstrong number : 
9474
Length of Digits is : 4
Sum is : 9474.0
The 9474 is an Armstrong number.

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Pagination with Bloc Pattern in Flutter

If_Else_Example