I've been trying to look up on Google how to put in an equation in my program but wasn't able to find any. How do you include:
x = ( -b + √b2 - 4ac ) / 2a
in the program?
Here's my code:
{
int a, b, c;
float x;
//statements
printf("Enter three integers: ");
scanf("%d %d %d", &a, &b, &c);
//computeforX
x = ( -b + √b2 - 4ac ) / 2a
printf("The value of x is %.1f", x);
return 0;
}
Assuming we're talking about C (or C++) here, you will need to investigate the sqrt function, and maybe also the pow function as well (although that's unnecessary because b-squared can be computed as b*b).
Note that you will need to convert all of your input values to float or double before you start the calculation, otherwise you will not get the intended result.
You need a table to allow you to translate:
a+b -> a+b
a-b -> a-b
a/b -> a/b
ab -> a*b
√x -> sqrt(x)
x² -> x*x (If you want to square something more complicated it might be best to use a temporary variable for the value to be squared, breaking your equation up into pieces.)
Note that if you divide an int by an int in C you get an int. So better convert those ints to doubles before dividing.
If we are dealing with C++ it would be something like
#include <iostream.h>
#include <cmath>
int main ()
{
//Declare Variables
double x,x1,x2,a,b,c;
cout << "Input values of a, b, and c." ;
cin >>a >>b >>c;
if ((b * b - 4 * a * c) > 0)
cout << "x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)" &&
cout << "x2 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a)";
if else ((b * b - 4 * a * c) = 0)
cout << "x = ((-b + sqrt(b * b - 4 * a * c)) / (2 * a)"
if else ((b * b - 4 * a * c) < 0)
cout << "x1 = ((-b + sqrt(b * b - 4 * a * c) * sqrt (-1)) / (2 * a) &&
cout << "x2 = ((-b + sqrt(b * b - 4 * a * c) * sqrt (-1)) / (2 * a);
return (0);
}
Now why do i have this wierd feeling I just did someone's first semester programming class' homework?
Granted its been years and I don't even know if that will compile but you should get the idea.
I am really depressed looking the quality of above answers and help, which has been given.
I hope to improve the content of this thread.
One can compile the C file below with the command line gcc file.c -o file -lm.
Herewith a possible solution in C:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(){
int da, db, dc;
double x, a,b,c;
//statements
printf("Enter three integers: ");
scanf("%d %d %d", &da, &db, &dc);
a = (double)da;
b = (double)db;
c = (double)dc;
//computeforX
x = (double) ( -b + sqrt(b * b) - 4 * a * c ) / ( 2 * a ) ;
printf("The value of x is %g \n", x);
return 0;
}
Related
#include <math.h>
#include <stdio.h>
main() {
int a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
scanf("%d%d%d", &a, &b, &c);
printf("The quadratic equation is %d*pow(x,2)+%d*x+%d=0", a, b, c);
if (pow(b, 2) - 4 * a * c >= 0) {
x1 = (-b + sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
x2 = (-b - sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
printf("the roots of the equation are x1=%d,x2=%d", x1, x2);
}
else
printf("roots of the equation in the form of x+iy and x-iy");
return 0;
}
Is this code alright for the given question, i had a bit confusion at that printing imaginary roots. could you please help
Use proper main prototype.
Use floating point numbers instead of integers
pow(b,2) == b*b
/ 2 * a -> / (2 * a)
int main(void) {
double a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
if(scanf("%lf %lf %lf", &a, &b, &c) != 3) { /* handle error */}
printf("\nThe quadratic equation is %f*x^2+%f*x+%f=0\n", a, b, c);
if (b*b - 4 * a * c >= 0) {
x1 = (-b + sqrt(b*b - 4 * a * c)) / (2 * a);
x2 = (-b - sqrt(b*b - 4 * a * c)) / (2 * a);
printf("the roots of the equation are x1=%f,x2=%f\n", x1, x2);
}
else
{
double r = -b / (2*a);
double z = sqrt(fabs(b*b - 4 * a * c));
printf("the roots of the equation are x1 = %f + i%f, x2 = %f - i%f", r,z,r,z);
}
}
https://gcc.godbolt.org/z/Ys1s8bWY7
I am relatively new to C, and am trying to improve myself in it. I made a calculator and added the quadratic equation solver to it, cause i know the formula of finding the roots. But i am faced with two problems.
Code:
#include <stdio.h>
#include <maths.h>
#include <stdlib.h>
#include <windows.h>
main(){
float A1, A2, A, B, C, ans, Z;
printf("Welcome to Quadratic Equation solver. Enter the coefficient of X^2, followed by\nthe coefficient of X, followed by the integer value.\n\nEnter values: ");
scanf("%f%f%f", &A, &B, &C);
CheckF = (B * B - 4 * A * C);
if (CheckF < 0) {
system("COLOR B4");
printf("This calculator HeX, currently cannot handle complex numbers.\nPlease pardon my developer. I will now redirect you to the main menu.\n");
system("pause");
system("cls");
system("COLOR F1");
goto Start;
} else if (CheckF >= 0) {
Z = pow(CheckF, 1/2);
A1 = (-B + Z)/(A+A);
A2 = (-B - Z)/(A+A);
if (A1 == A2) {
ans = A1;
printf("\nRoot of equation is %f (Repeated root)\n", ans);
Sleep(250);
} else if (A1 != A2) {
printf("Roots of equation are %f and %f \n", A1, A2);
Sleep(250);
}
}
}
Problem 1:
When i run the code and input 3 32 2, mathematically the output should be Roots of equation are -0.06287 and -10.6038, that i double checked with my sharp calculator. However, the output that i got was was off: Roots of equation are -5.166667 and -5.500000 i am totally unsure why is it not computing the correct roots of the equation.
Problem 2:
Some roots do not have the coefficient of X^2, for example (2X + 2), which can be solved to get repeated roots of -2, (6X - 3), which gives us that x is 0.5 repeated. However, according to the quadratic equation, which is divided by 2A, will never work, as it is divided by 0. What is the possible way out of this situation? Is it to check if A = 0 then do something else? Any help will be appreciable.
integer division
pow(CheckF, 1/2) is 1.0 as 1/2 is integer division with a quotient of 0.
// Z = pow(CheckF, 1/2);
Z = pow(CheckF, 1.0/2.0);
// or better
Z = sqrt(CheckF);
// Even better when working with `float`.
// Use `float sqrtf(float)` instead of `double sqrt(double)`.
Z = sqrtf(CheckF);
Best - re-write using double instead of float. Scant reason for using float here. double is the C goto floating point type.
Other issue
//#include <maths.h>
#include <math.h>
// main() {
int main(void) {
// CheckF = (B * B - 4 * A * C);
float CheckF = (B * B - 4 * A * C);
// goto Start;
Use an auto formater
I see some problems with the code. First, I suggest you to use double instead of float. They offer much better precision and an ideal calculator needs precision. Secondly, you do:
Z = pow(CheckF, 1/2);
You should use sqrt(CheckF) since there is a dedicated function in C for square roots! The following works for me so if you fix the above two problems, your code will probably work.
int main() {
double A1, A2, A, B, C, ans, Z;
printf("Welcome to Quadratic Equation solver. Enter the coefficient of X^2, followed by\nthe coefficient of X, followed by the integer value.\n\nEnter values: ");
A = 3;
B = 32;
C = 2;
double CheckF = (B * B - 4 * A * C);
if (CheckF >= 0) {
Z = sqrt(CheckF);
A1 = (-B + Z) / (A + A);
A2 = (-B - Z) / (A + A);
if (A1 == A2) {
ans = A1;
printf("\nRoot of equation is %f (Repeated root)\n", ans);
} else if (A1 != A2) {
printf("Roots of equation are %f and %f \n", A1, A2);
}
}
}
I was given a problem to write a C program which would solve the equation ax2+bx+c=0, where a, b and c are coefficients with double type. Any of the coefficients may be zero. In this problem it is unclear to me how to handle the double variables.
Here is my code. As for now, I know that my program can't distinguish between two roots and infinitely many roots. It also doesn't detect the "linear equation situation". How can I make it detect an infinite number of solutions? I was also advised in the comments to calculate the root with the minus before the discriminant if b > 0 and then use the Viet's theorem. I understand that it is because it is always more accurate to sum two numbers. I also guess I should do the exact opposite with b < 0. But what if b == 0 ? In this case, the program will not do anything. Or should I just include b == 0 in b < 0 and have b <= 0 ?
#include <stdio.h>
#include <math.h>
#include <float.h>
int main() {
double a, b, c, x1, x2;
scanf("%lf", &a);
scanf("%lf", &b);
scanf("%lf", &c); // just reading variables
//ax^2+bx+c=0
if ((b * b - 4 * a * c) < 0) {
printf("no");
} else {
x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); //calculating roots
x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
if ((fabs((a * x1 * x1 + b * x1 + c)) < DBL_EPSILON) & (fabs((a * x2 * x2 + b * x2 + c)) < DBL_EPSILON)) { //plugging the roots in
if (fabs((x1 - x2)) < DBL_EPSILON) { //checking if the roots are equal
printf("%lf", &x1); // if they are equal, we print only one of them
} else {
printf("%lf", &x1); // if they are not equal, we print both.
printf("\n %lf", &x2);
}
} else { // if there are no two valid roots
if ((fabs((a * x1 * x1 + b * x1 + c)) < DBL_EPSILON)) // we try to find one root.
printf("%lf", &x1);
if (fabs((a * x2 * x2 + b * x2 + c)) < DBL_EPSILON)
printf("%lf", &x2);
if ((fabs((a * x1 * x1 + b * x1 + c)) > DBL_EPSILON) & (fabs((a * x2 * x2 + b * x2 + c)) > DBL_EPSILON)) // if both of the plugged roots don't satisfy the equation
printf("no");
}
}
return 0;
}
Solve quadratic equation when coefficients may be 0
How can I make it detect an infinite number of solutions?
When a==0 && b == 0 && c == 0.
No DBL_EPSILON needed really anywhere in this code. See also #Eric Postpischil.
But what if b == 0 ?
if (b == 0) { // y = a*x*x + c
if (a) {
double dd = -c/a;
if (dd >= 0) {
double d = sqrt(d);
printf_roots("+/- roots", d,-d);
} else {
printf_roots("Complex roots", NAN, NAN); // Note NAN may not exist
}
} else if (c) { // y = 0*x*x + c, c != 0
printf_roots("No roots", NAN, NAN);
} else { // y = 0*x + 0
printf_roots("Infinite roots", -HUGE_VAL, HUGE_VAL);
}
Or should I just include b == 0 in b < 0 and have b <= 0 ?
Unless the coding goal requires a special output when b==0, I would only vector code on b==0 as a subtest when a==0 occurred.
if (a==0) {
if (b == 0) {
The quadric equation, like much FP code, can readily overflow and hit 0, both cases losing all precision.
Consider the code below: the unnecessary subtraction may cause overflow or truncation to 0 versus the second which may not. It is dependent on many things.
if ((b * b - 4 * a * c) < 0)
//
if (b * b < 4 * a * c)
Further, C allows various calculations to occur using wider math. Research FLT_EVAL_METHOD. Because of this, to prevent sqrt(value_less_than_0), code should calculate the discriminate and then test the object x that is going to be applied to sqrt(x).
//if ((b * b - 4 * a * c) < 0) {
// printf("no");
//} else {
// x1 = (-b + sqrt(b * b - 4 * a * c))
double discriminate = b * b - 4 * a * c;
if (discriminate < 0) {
printf("no");
} else {
double d = sqrt(discriminate);
x1 = (-b + d)
As to the idea of "calculate the root with the minus before the discriminant if b > 0 and then use the Viet's theorem", I'd suggest for improved retained precision the below which does not subtract like signed values.
double d = sqrt(discriminate);
// Note x1*x2 = c/a
if (b < 0) {
x2 = (-b + d)/(2*a);
x1 = c/a/x2;
} else {
x1 = (-b - d)/(2*a);
x2 = c/a/x1;
}
printf_roots("2 roots", x1, x2);
Notes on printf("%lf", &x1);. You are not compiling with all warnings enabled. Save time - enable them. Should be printf("%lf", x1); No &.
Further double is floating point. For FP code development use "%e", "%a" or"%g" to full see significant information.
printf("%g\n", some_double);
// or better
printf("%.*e\n", DBL_DECIMAL_DIG -1, some_double);
Since division by zero is not allowed, you have to split the problem into 4 cases :
a != 0:
this is case you treated in your code.
a == 0 && b != 0 :
This is a linear equation where the solution is x = -c/b
a == 0 && b == 0 && c != 0 : There's no possible value for x.
In this last case, a, b and c are equals to 0 : there's infinitly many solutions for x.
EDIT: comparisons with epsilon removed since they seem to be useless
There are some problems in your code:
you should check the return values of scanf() to avoid undefined behavior on invalid input.
you should use local variables for intermediary results to improve code readability
your printf statements are incorrect: you should pass the values of the double variables instead of their addresses: printf("%lf", &x1); should read:
printf("%f", x1);
Regarding the degenerate cases, you should just test those before trying to resolve the second degree equation.
Here is a corrected version:
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, delta, x1, x2;
if (scanf("%lf%lf%lf", &a, &b, &c) != 3) {
printf("invalid input\n");
return 1;
}
if (a == 0) {
// not a quadratic equation
if (b != 0) {
printf("one solution: %g\n", -c / b);
} else {
if (c != 0) {
printf("no solution\n");
} else {
printf("all real values are solutions\n");
}
}
} else {
delta = b * b - 4 * a * c;
if (delta < 0) {
printf("no real solution\n");
} else
if (delta == 0) {
printf("one double solution: %g\n", -b / (2 * a));
} else {
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("two solutions: %g, %g\n", x1, x2);
}
}
return 0;
}
I'd like for the program to solve my equation yet sadly it doesn't. Additionally, I'd want for it to print an answer depending on the value of x that I input in the equation. Please let me know how I would be able to print the answer or how I can program it so that the equation gives me an answer that I can then print.
/* Preprocessor directives */
#include <stdio.h>
#include <math.h>
/* Main program */
void main ()
{
/*
variable declaration section comments
l: length value
q: value of q
ei: value of ei
s: l devided by 2 since 0 < x < l/2
b: the length l (thus, 20)
z: 0
first_equation: The first equation pertaining to 0 < x < l/2
second_equation:The second equation pertaining to l/2 < x < l
*/
double x, first_equation, second_equation, l, q, ei, s, b, z;
l = 20.0;
q = 4000.0;
ei = 1.2 * (pow(10.0, 8.0));
s = l / 2.0;
b = l;
z = 0.0;
printf ("please enter the x-value\n");
scanf ("%lf", &x);
/* Deflection equations */
first_equation = ((q * x) / (384.0 * ei)) * ((9 * (pow(l, 3.0))) - (24.0 * l * (pow(x, 2.0))) + (16 * (pow(x, 3.0))));
second_equation = ((q * l) / (384.0 * ei)) * ((8 * (pow(x, 3.0))) - (24.0 * l * (pow(x, 2.0))) + (17 * (pow(l, 2.0)) * x) - (pow(l, 3.0)));
/* Determining what equation to use */
if (x >= z && x <= s)
printf ("\n first_equation\n\n");
else if (x > s && x <= b)
printf ("\n second_equation\n\n", second_equation);
else if (x < 0 || x > b)
printf ("\n invalid location\n\n");
return;
}
This...
printf ("\n second_equation\n\n", second_equation);
... does not print the second_equation variable: it provides it as an argument to printf, but printf only uses extra arguments as directed by %f or other conversion instructions embedded in the text provided as the first argument. You could write:
printf ("\n second_equation %f\n\n", second_equation);
You may want to do something similar for first_equation.
Alternatively [when I answered the question was tagged C++] you could use C++ I/O routines (scanf and printf are from the C library, and have a number of disadvantages, the most obvious here being that you have to remember funny letter codes like "lf" matching your data types)...
#include <iostream>
...at the very top of your file, then in your function write...
std::cout << "\n second_equation " << second_equation << "\n\n";
You could also use C++ I/O for input, replacing scanf with...
if (!(std::cin >> x))
{
std::cerr << "you didn't enter a valid number\n";
exit(1);
}
Your code is really unclear; but going by your question, you seem to want to be able to print your answer. In that case, here is the proper syntax
printf ("Answer: %d \n", yourAnswer); //if 'yourAnswer' is decimal or number
To use one of your code snippets, you'll have this:
printf ("\n second_equation: %d\n", second_equation);
#include <stdio.h>
int main() {
float a,c,d;
int b;
printf("Enter the float number: "); scanf("%f", &a);
a * 100 == b ;
b % 100 == c ;
c + a == d ;
printf("%f", d);
}
It prints 0.00.
Why is it doing this?
These statements:
a * 100 == b ;
b % 100 == c ;
c + a == d ;
are a series of comparisons. == is the equality comparison operator, while = is the assignment operator. Also, % is the modulus operator, and can only be used for integral operands. Perhaps you meant something more like
b = (float) ((int) (a * 100) % 100); /* fraction */
c = (float) ((int) a % 100) / 100.0; /* mantissa */
d = b + c;
Note that this is not necessarily good style, but should work.
To start with == is a comparison operator. To assign a value, you need to use =.
Assignment syntax in C is written as:
b = a * 100;
Try reading some tutorials on assignment operations and you should see where you are going wrong.
Here's one to get you started.