forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
automorphic_number.dart
63 lines (54 loc) · 1.12 KB
/
automorphic_number.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
57
58
59
60
61
62
63
/**
* Given a number and you need to check if a number is Automorphic or not.
* A number is called Automorphic if the square of the number ends
* with the number itself.
* Input:
* First line of input contains a number of integer data type.
* Output:
* A single line telling whether a number is Automorphic or not.
*
*/
import 'dart:io';
import 'dart:math';
int Check_Automorphic(int n) {
int square, temp, remainder, no_digits = 0;
temp = n;
square = n * n;
int flag = 10;
while (n != 0) {
n = (n / 10).floor();
no_digits++;
}
flag = pow(10, no_digits) as int;
remainder = square % flag;
if (remainder == temp)
return 1;
else
return 0;
}
void main() {
int num;
print("Enter the number: ");
num = int.parse(stdin.readLineSync()!);
int result = Check_Automorphic(num);
if (result == 1)
print("$num is a Automorphic number");
else
print("$num is not a Automorphic number");
}
/*
* Example:
* Input:
* 3
* Output:
* 3 is not a Automorphic number.
*
* Input:
* 25
* Output:
* 25 is a Automorphic number.
*/
/*
*Time complexity : O(n)
*Space complexity : O(1)
*/