fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Function to check Automorphic number
  5. bool isAutomorphic(int N)
  6. {
  7.  
  8. if(N < 0) N = -N;
  9.  
  10.  
  11. // Store the square
  12. int sq = N * N;
  13.  
  14. // Start Comparing digits
  15. while (N > 0) {
  16. // Return false, if any digit of N doesn't
  17. // match with its square's digits from last
  18. if (N % 10 != sq % 10)
  19. return false;
  20.  
  21. // Reduce N and square
  22. N /= 10;
  23. sq /= 10;
  24. }
  25.  
  26. return true;
  27. }
  28.  
  29. // Driver code
  30. int main()
  31. {
  32. int N = 5;
  33.  
  34. isAutomorphic(N) ? cout << "Automorphic"
  35. : cout << "Not Automorphic";
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5540KB
stdin
Standard input is empty
stdout
Automorphic