Integer Square Root

I am trying to write code a problem that calculates the integer square root by way of calculating the square root of every number from 1 until the square of the number if more then the entered number
so if the number is 16, i do 1x1, 2x2, 3x3, 4x4, then it stops and the display answer is 4, since 5x5=25 goes over 16. I am having trouble with a while statement for my integer function, below is what I have.
[code]
Put the code you need help with here.
int intSqrRoot(int num) {
int answer;
answer = (num/2);
return answer;
}
int main() {
cout<< "please enter a num between 2 and 1000: ";
int num;
int answer;
cin>> num;
cout<< endl;
answer = intSqrRoot(num);
if ((num>=2) && (num<=1000)) {
cout<< "the int square root of ";
cout<< num;
cout<< "is";
cout<< answer;
cout<< ".";
cout<< endl;
}
else {
cout<< "please follow directions";
cout<< endl;
}
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
using namespace std;

int main()
{

	int mult = 1;
	int myNum = 24;		// my test num

	do
	{
		cout << mult << "x" << mult << endl;
		mult++;

	} while ((mult*mult) < myNum);

	return 0;
}
Right now you're calling the function to do the square root before you validate that the number is in the valid range. Also - I'm not sure why you're doing dividing num by 2 in your calculation?

I think you're trying to do something like this?

Square Root function
---declare/initialize variable to store result of square root calculation
---declare/initialize variable to hold base number being squared (start at 1)
---while result is less than or equal to user number
------result = square of base
------increment base by 1
---return result

Main function:
---get user input
---while number is not between 2 and 1000
------prompt to re-enter
---call square root function with user number and store result
---output result
Topic archived. No new replies allowed.