-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkaperkar.cpp
More file actions
executable file
·54 lines (46 loc) · 937 Bytes
/
kaperkar.cpp
File metadata and controls
executable file
·54 lines (46 loc) · 937 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int nodigits(int n)
{
int count=0;
while(n>0)
{
count++;
n/=10;
}
return count;
}
// Complete the kaprekarNumbers function below.
void kaprekarNumbers(int p, int q) {
int present=0,dig,nod;
long int sq,l,r,poften;
for(long int i=p;i<=q;i++)
{
sq=i*i;
nod=nodigits(i);
//cout<<nod<<endl;
poften=pow(10,nod);
//cout<<poften<<endl;
r=sq%poften;
//cout<<r<<endl;
l=sq/poften;
// cout<<l<<"\n"<<r<<"\n"<<endl;
if(l+r==i){
cout<<i<<" ";
present=1;
}
}
if(!present)
cout<<"INVALID RANGE";
}
int main()
{
int p;
cin >> p;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int q;
cin >> q;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
kaprekarNumbers(p, q);
return 0;
}