View Full Version : help needed is C
The~Black~Skull
04-02-2009, 11:21 PM
I wrote a program in C to calulate the roots of a quadratic equation. I am having a problem with the imaginery part.
please help asap.
:confused::confused:
delta =(pow(b,2))-(4*a*c) ;
if ( delta < 0 )
{
X1= (-b+(sqrt(delta)))/(2*a);
X2= (-b-(sqrt(delta)))/(2*a);
printf(" The quadratic equation has 2 imaginary roots.\n");
printf(" root1 = %f i root 2 = %f i", X1,X2);
}
oesxyl
04-03-2009, 12:59 AM
I wrote a program in C to calulate the roots of a quadratic equation. I am having a problem with the imaginery part.
please help asap.
:confused::confused:
delta =(pow(b,2))-(4*a*c) ;
if ( delta < 0 )
{
X1= (-b+(sqrt(delta)))/(2*a);
X2= (-b-(sqrt(delta)))/(2*a);
printf(" The quadratic equation has 2 imaginary roots.\n");
printf(" root1 = %f i root 2 = %f i", X1,X2);
}
put your code between [ code] and [ /code] tags without spaces. You can edit your original post to do that. Thank you.
if delta is negative, it's sqrt is undefined, so you must make it positive, or use absolute value and you don't need to write again same stuff for delta >= 0 .
delta =(pow(b,2))-(4*a*c) ;
if(delta < 0){
X1= (-b+(sqrt(-delta)))/(2*a);
X2= (-b-(sqrt(-delta)))/(2*a);
printf(" The quadratic equation has 2 imaginary roots.\n");
printf(" root1 = %f i root 2 = %f i", X1,X2);
}
the roots if delta < 0 are two complex numbers -b/(2*a) + i sqrt(-delta)/(2*a) and -b/(2*a) - i sqrt(-delta)/(2*a). Your code is wrong.
best regards
The~Black~Skull
04-03-2009, 11:05 AM
Thank you for your help. I know that we should right the equation of the roots -b/(2*a) + i sqrt(-delta)/(2*a) and -b/(2*a) - i sqrt(-delta)/(2*a).
but how do I put it in the code. either we define I or put it with the printf function.
The whole program is
#include <stdio.h>
#include <math.h>
int main (void)
{
double a,b,c;
double delta, X1, X2 ;
printf(" Enter a , b , c of the quadratic eq. > ");
scanf (" %lf%lf%lf.\n", & a, &b, &c);
delta =(pow(b,2))-(4*a*c) ;
if ( delta < 0 )
{
X1= ( -b/(2*a) - i sqrt(-delta)/(2*a);
X2= ( -b/(2*a) + i sqrt(-delta)/(2*a);
printf(" The quadratic equation has 2 imaginary roots.\n");
printf(" root1 = %f root 2 = %f", X1,X2);
}
else if (delta == 0)
{
X1= (-b)/(2*a);
printf ("The quadratic equation has double root.\n");
printf ("root1=root2= %f", X1);
}
else if (delta > 0 )
{
X1= ((-b)/(2*a))+((sqrt(delta))/(2*a));
X2= ((-b)/(2*a))-((sqrt(delta))/(2*a));
printf("The quadratic equation has 2 distinct roots.\n");
printf(" root1 = %f root 2 = %f ", X1,X2);
}
else
return (0) ;
}
oesxyl
04-03-2009, 11:22 AM
do not use icode tags for code, please, use code tags instead, :)
I would compute real and imaginary part and based on the value of delta I will output the results using printf. Someting like that:
printf("%f + i %f", repart, impart);
printf("%f",repart + impart);
of course expanded to your case.
best regards
anime258
04-09-2009, 09:52 AM
I think it is the simplest way :)
;VN
#include <stdio.h>
#include <math.h>
int main (void){
double a,b,c;
double delta, X1, X2 ;
printf(" Enter a , b , c of the quadratic eq. > ");
scanf (" %lf%lf%lf.\n", & a, &b, &c);
delta =(pow(b,2))-(4*a*c) ;
if ( delta < 0 ) {
printf(" The quadratic equation has 2 imaginary roots.\n");
printf(" root1 = %f -%fi",-b/(2*a),sqrt(-delta)/(2*a));
printf(" root2 = %f +%fi",-b/(2*a),sqrt(-delta)/(2*a));
}
else if (delta == 0) {
X1= (-b)/(2*a);
printf ("The quadratic equation has double root.\n");
printf ("root1=root2= %f", X1);
}
else {
X1= ((-b)/(2*a))+((sqrt(delta))/(2*a));
X2= ((-b)/(2*a))-((sqrt(delta))/(2*a));
printf("The quadratic equation has 2 distinct roots.\n");
printf(" root1 = %f root 2 = %f ", X1,X2);
}
return 0;
}
Anime Shippuuden :D:D (http://www.animeshippuuden.com)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.