| crackerjr (1) | |
|
This is what i have written so far : #include<iostream> #include<cmath> using namespace std; int main() { int n; cout<<"Please enter an integer :"<<endl; cin>>n; **************************** This is my objective : Write a program which reads an integer n entered by the user and prints true if n is a perfect square and false if it is not. Im not sure as to how to go about doing this with an If statement / boolean operations and I'd appreciate all the help I can get :) *I know how to do an IF statement and how to use booleans. I'm just not clear on how to get it to check to see if it is a perfect number or not* Thanks | |
|
Last edited on
|
|
| Aceix (455) | |||
After 10 minutes thinking and working(on paper) with my mathematical brain and God helping me, this is the formula/code I got:
Hope it HELPS!!! | |||
|
|
|||
| Aceix (455) | |||
Also, another version to tell what it's square root is:
| |||
|
|
|||
| doug4 (224) | |||
|
The code that @Aceix provided is a bit of overkill. However, he (or she?) mentioned the sqrt() function found in <cmath>.The following code segment:
will give you the largest integer which is <= the square root of x. Form there you can check whether x is a perfect square.
| |||
|
|
|||