-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path58thProgram_Example_14.cpp
108 lines (104 loc) · 2.87 KB
/
58thProgram_Example_14.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/******************************************************************************
Automorphic Number :- Automorphic Number is a number whose square's last digit
are equal to the number itself.
i.e. 25 = 25 x 25 = 625 and 625 is automorphic number i.e.
last digit of 625 is 25.
Hence 25 is an automorphic number .
Similarly , 5 = 5 x 5 = 25 hence 5 is an automorphic number.
****************************************************************/
#include <iostream>
using namespace std;
int main(){
int n, sq;
cout<<"Enter a number :";
cin>>n;
sq = n * n;
int c =0;
int temp = sq;
int temp1 = n;
int digit;
//Count the digits of the number, suppose n =25 then c=2.
while(n!=0){
digit = n%10;
n = n / 10;
c = c+1;
}
int sum =0;
int s =0;
int m;
//Find the last digit of the square, suppose n =25, sq = 625, then s=25 .
//But here it will return the reverse of last digit = 52.
for(int i=1; i<=c;i++){
m = temp%10;
sum = sum * 10 +m;
temp = temp/10;
s = s+1;//count the number of digit
}
int sum1 = 0;
int r;
//Reverse the reversed number
for(int i=1; i<=s ; i++){
r = sum % 10;
sum1 = sum1*10 + r;
sum = sum/10;
}
//Check if the square is automorphic or not.
if(sum1 == temp1){
cout<<"The number is automorphic";
}
else{
cout<<"The number is not automorphic";
}
return 0;
}
/*******************************************************************************
* Working of the program :-
*
* Enter a number :25
* sq = 25 x 25 = 625
* c=0
* temp = sq =625
* temp1 = n = 25
*
* count the digits of the number, suppose n =25 then c=2.
* As we have to see the number exists in its squares last digits
* while n!=0
* digit= n % 10 = 25 % 10 = 5
* n = n / 10 = 25 / 10 = 2
* c = c+1 = 0+1 = 1
*
* digit= n% 10 = 2 % 10 = 2
* n= n / 10 = 2 / 10 = 0
* c = c+1 = 1+1 = 2
*
* As n =0 , it terminates the loop.
*
* Find the last digit of sq
* int 1 to 2 :
* m = temp % 10 = 625 % 10 = 5
* sum = sum * 10 + m = 0 * 10 + 5 = 5
* temp = temp/10 = 625/10 = 62
* s = s+1 = 0+1 = 1
*
* int 2 to 2 :
* m = temp % 10 = 62 % 10 = 2
* sum = sum * 10 + m = 5 * 10 + 2 = 52
* temp = temp/10 = 62/10 = 6
* s = s+1 = 1+1 = 2
* Hence we get sum = 52.
*
* Reverse the reversed number
* int 1 to 2 :
* r = sum % 10 = 52 % 10 = 2
* sum1 = sum1*10 + r = 0*10 + 2 = 2
* sum = sum/10 = 52/10 = 5
*
*
* int 2 to 2 :
* r = sum % 10 = 5 % 10 = 5
* sum1 = sum1*10 + r = 2*10 + 5 = 25
* sum = sum/10 = 5/10 = 0
*
* Hence sum1 = temp1 = 25 , hence it is automorphic number.
*
* ***************************************************/