square root

This is a discussion on square root within the C++ Programming forums, part of the General Programming Boards category; how can i write a program which will find the square root of a number?...

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    square root

    how can i write a program which will find the square root of a number?
    AIM: MarderIII

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Well generally you'd use the ^ symbol to square things but I don't think you can in C++, so I'd probably do:

    Code:
    int Square(int Number)
    {
        return Number * Number;
    }
    Also, I think there would already be functions to do this, probably in the math.h header or something. You might want to look that up.
    Last edited by nickname_changed; 02-12-2003 at 01:00 AM.

  3. #3
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    That of course would square the number, not provide the square root of the number.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    LOL Sorry! It's a bit late over here.

    That should read

    return Number / Number;

    Rather than
    return Number * Number;

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    return Number / Number;
    Number / Number is 1 unless Number is 0 in which case it's undefined.

    To get the square root of a number you can use the sqrt function in <cmath>
    Code:
    #include <cmath>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double n = 9.0;
        cout << "sqrt(" << n << ") = " << sqrt(n) <<'\n';
    }
    - lmov

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Okay, I just lost all credibility on this forum.

    Better go and make a new identity.

  7. #7
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    And since I dont have <cmath>, what do I do?
    AIM: MarderIII

  8. #8
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Try <math.h>. Oh, you don't have that either?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  9. #9
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    nah i have that
    AIM: MarderIII

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Square Root ?!?
    By Tm687 in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 03:38 PM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 01:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21