int sqrt(int number); - This line is a function declaration for a function named sqrt, which takes an integer parameter named number and returns an integer value.
sq_root = sqrt(1000); - This line assigns the result of calling the sqrt function with the value 1000 to a variable named sq_root.
int sqrt(int number) - This line starts the definition of the sqrt function. It specifies that the function takes an integer parameter named number and returns an integer value.
int guess=5, i=1; - This line declares and initializes two integer variables named guess and i, with initial values 5 and 1 respectively.
for(i=1; guess=5; i != guess;) - This line starts a for loop with i set to 1, guess set to 5, and the condition i != guess. This creates an infinite loop because the condition is never false.
i = guess; - This line sets the value of i to the current value of guess.
guess = (i + (10000/i))/2; - This line updates the value of guess using the Babylonian method to approximate the square root of 10000.
return guess; - This line ends the sqrt function and returns the value of guess as the result of the function.
int sqrt(int number);
sq_root = sqrt(1000);
int sqrt(int number)
int guess=5, i=1;
for(i=1;guess=5;i != guess;)
{
i = guess;
guess = (i + (10000/i))/2;
}
return guess;
}
what does each line do?
1 answer