Thread: Square Root??

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    44

    Square Root??

    If i have a number and i want to find the square root of it in a c program how could i go about this.

    Thank,
    Dangerous Dave

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The command sqrt() in math.h

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    44
    Thanx guys,

    here is the code of the program i made.

    -------------------------------------------------------------------------------

    /* MADE BY DANGEROUS DAVE 2001 with complments to matt*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    void main()

    {
    float a, b, c, s, u, v, w, x, y, z;
    float ans1, ans2;

    printf("Progran to find the roots of a quadratic equation useing the B rule\n");

    printf("please enter A:\n");
    scanf ("%f" , &a);
    printf("Please enter B:\n");
    scanf ("%f" , &b);
    printf("Please enter C:\n");
    scanf ("%f" , &c);

    if (b<0)
    {
    b = b*-1;
    }
    x = b*b;
    y = 4*a*c;
    z = 2*a;
    s = x-y;
    s = sqrt (s);
    ans1 = (b+s)/z;
    ans2 = (b-s)/z;

    printf("\nYour answers are (%f , %f)\n\n" , ans1 , ans2);

    system("PAUSE");

    }

    ------------------------------------------------------------------------------

    any sugestions would be greatly recieved.

    Thanx,
    Dangerous Dave

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Your code erroneous , check the snippet below

    if (b<0)
    {
    b = b*-1;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Subtraction is easier than multiplication.

    if( x < 0 ) { x = 0 - x; }

    Quzah.

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    why the 0 ?
    why not x = -x ?
    anyway his program does not compute the roots of the equation
    ax^2+bx+c
    the output on the inputs a=1 b=2 c=1 and a=1 b=-2 c=1 are exactly the same due to the above portion of the code , also a=0 is not considered .

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, 09:10 AM
  4. Square Root ?!?
    By Tm687 in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 04:38 PM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM