-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
automorphic_number.cpp
69 lines (57 loc) · 1.19 KB
/
automorphic_number.cpp
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
64
65
66
67
68
69
/**
* 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.
*
*/
#include <bits/stdc++.h>
using namespace std;
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;
no_digits++;
}
flag = pow(10, no_digits);
remainder = square % flag;
if (remainder == temp)
return 1;
else
return 0;
}
int main()
{
long long int num;
cout << "Enter the number: " << endl;
cin >> num;
int result = Check_Automorphic(num);
if (result == 1)
cout << num << " is a Automorphic number. " << endl;
else
cout << num << " is not a Automorphic number. " << endl;
}
/*
* 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)
*/