-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Kaprekar_no.c
68 lines (61 loc) · 1.06 KB
/
Kaprekar_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
C program to check whether a number is kaprekar number or not
Kaprekar number is a number whose square when divided into two parts and then added gives the original number
Eg: 45 (45^2 = 2025 -->20+25 = 45)
*/
#include<stdio.h>
#include<math.h>
#include<stdbool.h>
bool kaprekar (int n);
int main ()
{
int n;
printf ("Enter the number to check:\n");
scanf ("%d", &n);
if (kaprekar (n) == true)
{
printf ("Kaprekar Number!");
}
else
{
printf ("Not Kaprekar Number !");
}
return 0;
}
bool kaprekar (int n)
{
if (n == 1)
return true;
int s = n * n;
int t = 0;
while (s)
{
t = t + 1;
s = s / 10;
}
s = n * n;
for (int c = 1; c < t; c++)
{
int f, x1, x2, sum;
f = pow (10, c);
if (f == n)
continue;
x1 = s / f;
x2 = s % f;
sum = x1 + x2;
if (sum == n)
return true;
}
return false;
}
/*
SampleInput-Output1:
Enter the number to check:
297
Kaprekar Number!
SampleInput-Output2:
Enter the number to check:
12
Not Kaprekar Number!
Time Complexity: O(n)
*/