forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautomorphic_no.c
51 lines (44 loc) · 817 Bytes
/
automorphic_no.c
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
/*
C program to check whether the number is automorphic or not.
Automorphic number is a number whose square ends with the original number itself
*/
#include <stdio.h>
int automorphic_num(int);
int main()
{
int n, l;
printf("Enter the number to check:\n");
scanf("%d", &n);
l = automorphic_num(n);
if (l == n)
printf("\nAUTOMORPHIC NUMBER !");
else
printf("\nNOT AUTOMORPHIC NUMBER !");
return 0;
}
// Function to check if the number is automorphic or not
int automorphic_num(int n)
{
int s, temp, l;
temp = n;
s = n * n;
int flag = 1;
while (n != 0)
{
flag = flag * 10;
n = n / 10;
}
l = s % flag;
return l;
}
/*
Sample Input-Output:1
Enter the number to check:
7
NOT AUTOMORPHIC NUMBER !
Sample Input-Output:2
Enter the number to check:
5
AUTOMORPHIC NUMBER !
Time Complexity: O(n)
*/