-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifiedKaprekarNumber.cpp
More file actions
68 lines (62 loc) · 1.23 KB
/
ModifiedKaprekarNumber.cpp
File metadata and controls
68 lines (62 loc) · 1.23 KB
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
/*A modified Kaprekar number is a positive whole number with a special property.
If you square it, then split the number into two integers and sum those integers,
you have the same value you started with.
*/
#include<stdio.h>
long splits(long sq);
long countDig(long num);
long square(long num);
long mypow(long x,long y);
int flag=0;
int main()
{
long p,i,q;
// printf(" %d" , mypow(10,2));
// return 0;
//printf("%ld",sizeof(long ) );
// printf("Enter num..");
scanf("%ld %ld",&p,&q);
for(i=p;i<=q;i++)
{
long sq =square(i);
long res=splits(sq);
if(res == i)
{
flag = 1;
printf("%ld ",i);
}
}
// printf("Digits(%d) = %d",num,digits);
if(flag==0)
{
printf("INVALID RANGE");
}
return 0;
}
long square(long num)
{
return (num * num);
}
long mypow(long x,long y)
{
long i;
int res = x;
for( i=1;i<y;i++)
res = res * x;
return res;
}
long splits(long sq)
{
long digits = countDig(sq);
long den= mypow(10, (digits+1)/2);
long right = sq%den;
long left = sq/den;
return (left + right);
}
long countDig(long num)
{
if(num ==0)
return 0;
num=num/10;
return 1+countDig(num);
}