NVIDIA Interview Question
Software Engineer / DevelopersCountry: United States
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c;
printf("\n enther the number square root who wanna found\n");
scanf("%f",&a);
int i,j;
b=a;
for(i=1;i<10;i++)
{
b=0.5*(b+a/b);
}
printf("\nsqrt of %f is %f\n\n",i,a,b);
}
Float sqrt(float num)
{
Float x;
If(num >0)
{
for(i=0;I<20;i++)
{
x=(x*x+num)/(2*x);
}
Return x;
}
}
class Solution {
public int mySqrt(int target) {
long start=1;
long ans=0;
long end=target;
if(target==0)
return target;
while(start<=end)
{
long mid=start+(end-start)/2;
if(mid*mid==target)
{
return (int)mid;
}
else if(mid*mid<target)
{
ans=mid;
start=mid+1;
}
else
{
end=mid-1;
}
}
return (int)ans;
}
}
}
float SquareRoot(float num)
- Anonymous May 15, 2012{
if(num >= 0)
{
float x = num;
int i;
for(i = 0; i < 20; i ++)
{
x = (((x * x) + num) / (2 * x));
}
return x;
}
}