/*Write a Boolean function that accepts a positive integer parameter from
the caller and returns true if the integer is a perfect square and false
if not. Test your function by writing a main that reads a positive integer,
calls the functions to see whether it is a perfect square, and outputs an
appropriate message. Note that all input and output are to be done in
main, not in the function.
*/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
bool square_root (int n)
{
int k = (sqrt (n));
double kk = (sqrt (n));
double result = k -kk;
while (result > 0)
{return true;
}
return false;
}
int main()
{
int n;
cout << "Please enter a number and I will determine whether it is a" << endl;
cout << "perfect square or not: " << endl;
cin >> n;
if (square_root(true))
cout << n << " is a perfect square" << endl;
else
cout << n << " is not a perfect square" << endl;
system ("pause");
return 0;
}
|
is_perfect_square(), since the purpose of the function is to determine if its argument is a perfect square.return (result > 0);sqrt (n) twice. Just do it once, assigning the result to a double (kk). Then set result = kk - (int)kk;