2.4 Code Practice: Question 1 write the code to input a number and print the square root. Use the absolute value function to make sure that if the user enters a negative number, the program does not crash. Example: Enter a number: -16. Output 4.0​

Respuesta :

Answer:

// here is code in C++.

#include <bits/stdc++.h>

using namespace std;

//main function

int main()

{

   // variable

  int n;

  cout<<"enter a number:";

  // read the input

  cin>>n;

  // find the square root of the absolute value

  float square_root=sqrt(abs(n));

  // print the square_root

  cout<<"square root of "<<n<<" is: "<<square_root<<endl;

   return 0;

}

Explanation:

Read a number from user and assign it to "n".Then find its absolute value with abs() function.Then calculate the square root of the absolute value with sqrt() function.Print the square root of the number.

Output:

enter a number:-17

square root of -17 is: 4.12311

Answer:

import math

num=int(input("Enter a number: "))

x=math.fabs(num)

y=math.sqrt(x)

print(y)

Explanation: It will get the square root, then it will get the absolute value of the square root.

ACCESS MORE