How to Sum of Square Numbers in C++ Program ?

  C & C ++ Interview Q&A

Dear readers, today read in this article how to sum square numbers in a C ++ program?

If you have a non-negative integer c, we must decide whether the two integers a and b are such that it satisfies a ^ 2 + b ^ 2 = c.

So, if the input is the same as 61, the output will be true, as 61 = 5 ^ 2 + 6 ^ 2.

Example for Sum of Square Numbers in C++

#include <bits/stdc++.h>
using namespace std;
class sumsqureNumProg {
public:
   bool isPerfect(int x){
      long double sr = sqrt(x);
      return ((sr - floor(sr)) == 0);
   }
   bool judgeSquareSum(int c) {
      if (c == 0)
         return true;
      int b;
      for (int i = 0; i < ceil(sqrt(c)); i++) {
         b = c - i * i;
         if (isPerfect(b))
            return true;
      }
      return false;
   }
};
main(){
   sumsqureNumProg ob;
   cout << (ob.judgeSquareSum(61));
}

Output

1

LEAVE A COMMENT