How to calculate square root in C++

3 Answers

0 votes
#include <iostream>
#include <iomanip>
#include <cmath>
 
int main()
{
    double value = 121.0;
 
    std::cout << "The square root of " << value << " is " << std::fixed << 
                                                        std::setprecision(2) << 
                                                        sqrt(value);
 
}
 


/*
run:
 
The square root of 121 is 11.00
 
*/

 



answered Feb 18, 2016 by avibootz
edited Mar 2, 2024 by avibootz
0 votes
#include <iostream>
#include <iomanip>
#include <cmath>
 
int main()
{
    double value = 121.0;

    std::cout << "The square root of " << value << " is " << std::fixed << 
                                                        std::setprecision(2) << 
                                                        pow(value, 0.5);
 
}
 


/*
run:
 
The square root of 121 is 11.00
 
*/

 



answered Feb 18, 2016 by avibootz
edited Mar 2, 2024 by avibootz
0 votes
#include <iostream>
#include <cmath>
 
int main() {
    int a = 9;
  
    std::cout << sqrt(a) << "\n";
  
    int b = 85;
  
    std::cout << sqrt(b);
}
  
  
  
  
/*
run:
      
3
9.21954
  
*/

 



answered Mar 2, 2024 by avibootz
...