Using C++
Write a template function that accepts an integer
parameter and returns its integer square root.
The function should return -1, if the argument passed is not
integer.
Demonstrate the function with a suitable driver program .
The answer to this question is as follows:
The code is as follows:
#include<bits/stdc++.h>
using namespace std;
int Sqrt(int num)
{
if (num == 0 || num == 1)
return num;
int x=num;
int y=(x+1)/2;
while(y<x)
{
x=y;
y=(x+num/x)/2;
}
return x;
}
int main()
{
int x = 20;
cout << Sqrt(x) << endl;
return 0;
}
The input and output are provided in the screenshot below:
Get Answers For Free
Most questions answered within 1 hours.