-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaprekar.c
71 lines (68 loc) · 1.25 KB
/
kaprekar.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
69
70
71
#include<stdio.h>
main()
{
long int n,k,temp,len,tri,len1,first,second,flag=0;
long int s,a[20];
scanf("%ld%ld",&n,&k);
/*if(n==400 && k==700)
{
printf("INVALID RANGE");
exit(0);
}*/
// printf("%ld %d\n",n,k);
while(n<=k)
{
len=0;
len1=0;
temp=n;
tri=n;
while(tri!=0)
{
len1++;
tri=tri/10;
}
s=temp*temp;
int i=0;
while(s!=0)
{
a[i]=s%10;
//printf("%ld",a[i]);
len++;
s=s/10;
i++;
}
//printf("\n");
int j=i;
i=0;
int c=0;
first=0;
second=0;
int z=1;
while(c<len1)
{
first=first+(a[i]*z);
//printf("%ld\n",first);
i++;
z=z*10;
c++;
}
z=1;
while(i<j)
{
second=second+(a[i]*z);
i++;
z=z*10;
}
// printf("%ld %ld %ld\n",temp,second,first);
if((first+second)==temp )
{
printf("%ld ",temp);
flag=1;
}
n++;
}
if(flag==0)
{
printf("INVALID RANGE");
}
}