Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int suma (int a)
- {
- int k=0;
- while (a>0)
- {
- k = k + (a%10)*(a%10);
- a = a / 10;
- }
- return k;
- }
- bool srecan (int n)
- {
- bool p = false;
- int t, s, i;
- t = suma(n);
- if (t==1)
- p = true;
- s = t;
- i = n;
- do
- {
- s = suma(s);
- if (s==1)
- p = true;
- i = i - 1;
- }
- while ((s!=t)&&(s!=1)&&(i>0));
- return p;
- }
- main()
- {
- int n, x = 0, i;
- cin >> n;
- i = n - 1;
- while ((i>0)&&(x==0))
- {
- if (srecan (i) == true)
- x = i;
- i = i - 1;
- }
- cout << x << endl;
- system ("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment