Examveda
Examveda

Which of the following C++ expressions will find the square root of the number 16?

A. pow (16, 2)

B. sqroot (16)

C. sqrt (16, 2)

D. sqrt (16)

Answer: Option D

Solution(By Examveda Team)

sqrt is a function from the C++ standard library that calculates the square root of a given number. In this case, the expression sqrt(16) calculates the square root of 16.

The function pow from the C++ standard library calculates the power of a number, i.e., it raises a number to a specified power. The expression pow(16, 2) calculates 16 raised to the power of 2, which is 256, and is not the square root of 16.

sqroot is not a standard C++ function and will result in a compile-time error.

sqrt (16, 2) is not a correct syntax for the sqrt function in C++, which takes only one argument, the number for which the square root is to be calculated.

So, the correct expression to find the square root of 16 in C++ is sqrt(16).

C++ program to find out square root of 16
#include <iostream>
#include <cmath>
using namespace std;
int main() {
  cout << "Square root of 16 = ";   
  // print the square root of 16
  cout << sqrt(16);
 return 0;
}

Here is an explanation of the code:

1. #include <iostream> - This line includes the input/output stream library, which provides the cout function that is used later in the code to print the output.
2. #include <cmath> - This line includes the C++ mathematics library, which provides mathematical functions such as sqrt() that is used later in the code to calculate the square root.
3. using namespace std; - This line is used to make all the elements of the standard C++ library available to the program.
4. int main() - This is the main function of the program. All C++ programs must have a main function, which is where the program starts executing.
4. cout << "Square root of 16 = "; - This line outputs the string "Square root of 16 =" to the console.
6. cout << sqrt(16); - This line calculates the square root of 16 using the sqrt() function from the C++ mathematics library and outputs the result to the console.
7. return 0; - This line is the return statement of the main function. It returns 0, indicating that the program has executed successfully.

In summary, this program calculates the square root of 16 and outputs the result to the console.


Join The Discussion

Related Questions on Object Oriented Programming Using C Plus Plus

A default catch block catches

A. all thrown objects

B. no thrown objects

C. any thrown object that has not been caught by an earlier catch block

D. all thrown objects that have been caught by an earlier catch block