Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Use the code: #include <iostream> using namespace std; /* Type your code her

ID: 3857634 • Letter: C

Question

C++

Use the code:

#include <iostream>
using namespace std;

/* Type your code here. */

int main()
{
   int num;
   cout << "Please enter a number between 2 and 1000: ";
   cin >> num;
   cout << num << endl;
   if ((num < 1) || (num > 1000))
       cout << "Please follow the directions!" << endl;
   else {
       int answer;
       answer = intSqrRoot(num);
       cout << "The integer square root of ";
       cout << num;
       cout << " is ";
       cout << answer;
       cout << ".";
       cout << endl;
   }
}

Ask the user for a number between 2 and 1000. Using a loop, calculate the integer square root by calculating the square of every number from 1 until the square of the number is more than the number entered. The previous number is the integer square root. You must write a function called intSqrRoot which calculates the answer and returns it to main. See the template given. Sample run: Please enter a number between 2 and 1000: 16 The integer square root of 16 is 4. The numbers in the square brackets are intermediate values.

Explanation / Answer

#include <iostream>

using namespace std;

/* Type your code here. */

int intSqrRoot(int num)

{

int square = 1 * 1;// Start with 1

int count = 1;// Counter increment with each iteration

cout << "[" << count << "]";// Intermediate numbers

while (square <= num)//run until square is greater than number

{

if (square < num)

{

cout << "[" << count << "]";// Intermediate numbers

}

count++;//Increment counter

square = count * count;//update square of counter

}

cout << " ";

return count - 1;//return previous counter

}

int main()

{

int num;

cout << "Please enter a number between 2 and 1000: ";

cin >> num;

// cout << num << endl;

if ((num < 1) || (num > 1000))

cout << "Please follow the directions!" << endl;

else {

int answer;

answer = intSqrRoot(num);

cout << "The integer square root of ";

cout << num;

cout << " is ";

cout << answer;

cout << ".";

cout << endl;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote