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,
This is a discussion on Square Root?? within the C Programming forums, part of the General Programming Boards category; If i have a number and i want to find the square root of it in a c program how ...
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
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
Your code erroneous , check the snippet below
if (b<0)
{
b = b*-1;
}
Subtraction is easier than multiplication.
if( x < 0 ) { x = 0 - x; }
Quzah.
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 .