-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifiedkaprekar.cpp
More file actions
96 lines (94 loc) · 1.45 KB
/
modifiedkaprekar.cpp
File metadata and controls
96 lines (94 loc) · 1.45 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
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
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
char a[100000],b[100000],l[100000],r[100000];
void toarray(long long int k,int base){
long long int temp;
int i=0;
while(k!=0){
temp=k%10;
a[i]=temp;
k/=10;
cout<<"ar"<<a[i]<<"\t";
i++;
}
}
long long int tonumber(char* a[]){
int i=0,flag=0;
long long int temp=0,j=1;
while(a[i]='\0'){
flag++;i++;
}
cout<<"flag"<<flag;
i=0;
while(a[i]!='\0'){
j=j*pow(10,flag-1-i);
temp+=((int)a[i])*j;
cout<<a[i];
i++;
}
cout<<temp;
return temp;
}
int kaprekar(long long int k){
int x,d=0,flag=0;
long long int sq,left,right;
sq=k*k;
// cout<<sq<<endl;
toarray(k,10);
toarray(sq,10);
// cout<<sq<<endl;
int i=0;
while(b[i]!='\0'){
// cout<<b[i];
flag++;
i++;
}
// cout<<sq<<endl;
i=0;
while(a[i]!='\0'){
d++;
i++;
}
if (flag-d==d){
for(int j=0;j<d;j++)
l[j]=b[j];
for(int k=d,x=0;k<flag;k++,x++)
r[x]=b[k];
}
else if(flag-d==d-1){
//cout<<flag<<"\t"<<d;
for(int j=0;j<d-1;j++)
l[j]=b[j];
for(int k=d-1,x=0;k<flag;k++,x++)
r[x]=b[k];
}
left=tonumber(l);
right=tonumber(r);
//sq=atoi(b);
// cout<<sq;
cout<<endl<<left<<"\t"<<right;
if(left+right==k)
return 1;
else
return 0;
}
int main(){
long long int x;
x=kaprekar(99);
/*long long int low,up;
int f1,i;
cin>>low>>up;
for(long long int v=low;v<=up;v++){
i=kaprekar(v);
if(i==1){
cout<<v<<" ";
f1++;
}
}
if(f1==0){
cout<<"INVALID RANGE";
}*/
return 0;
}