This question already has answers here:
Preventing console window from closing on Visual Studio C/C++ Console application
(24 answers)
Closed 5 months ago.
#include <stdio.h>
#include <math.h>
int main(){
float a;
float b;
float c;
printf("this program will solve you any equation from this type: ax^2+bx+c=0.\n");
printf("First of all enter a, b and c:\n");
printf("a:"); scanf("%f", &a);
printf("b:"); scanf("%f", &b);
printf("c:"); scanf("%f", &c);
if (a==0){
printf("now your equation is:\n");
printf("%.1fx+", b); printf("%.1f=0\n", c);
float x3 = -c/b;
printf("the solution for this equation is: %f", x3);
}
else{
printf("now your equation is:\n");
printf("%.1fx^2+", a); printf("%.1fx+", b); printf("%.1f=0\n", c);
float d = b*b-4*a*c;
printf("yy");
float x1;
float x2;
if(d > 0){
printf("delta is greater than 0! the equation have 2 solutions!\n");
float x1 = (-b+sqrt(d))/(2*a);
float x2 = (-b-sqrt(d))/(2*a);
printf("the first solution is: %.2f\n", x1);
printf("the second solution is: %.2f\n", x2);
}
else if (d == 0)
{
printf("delta equals 0 the equation have a doubled solution!\n");
float x1 = -b / 2*a;
printf("the doubled solution is: %f", x1);
}
else if (d < 0)
{
printf("delta is less than 0! the equation have no solutions in R :(");
}
}
return 0;
}
it works perfectly when i run it in vs code terminal thing but when i open the batch file it exits after entering the value of c
edit: the code i posted is only the start and the remaining code is 100% correct because i test it many times in terminal in vs code and it works
The file is correctly executed. When you open the batch file, the program reaches the end at the return 0 istruction (immediatly after you enter c). This operation is so fast that apparently is invisible, but the program is executed correctly. The reason why you can see it into the terminal, is because the terminal doesn't clear the prints.
Related
This question already has answers here:
Reading in double values with scanf in c
(7 answers)
Closed 4 years ago.
I have a piece of code in C, which is supposed to compute the circumference.
No matter what I put in for variable Z when asked for it, it always prints 0.000000
Any ideas?
#include <stdio.h>
int main()
{
double pi = 3.1415926;
double z = 0.0;
printf("What is the radius of the circle? \n ");
scanf("%1f", &z);
double c = 2.0 * pi * z;
printf("The circumference is %1f", c);
return 0;
}
Change %1f to %lf.
Like so:
#include <stdio.h>
int main()
{
double pi = 3.1415926;
double z = 0.0;
printf("What is the radius of the circle? \n ");
scanf("%lf", &z);
double c = 2.0 * pi * z;
printf("The circumference is %lf", c);
return 0;
}
For reading into z, a double, you have to use scanf("%lf", &z) instead of "%1f".
You were very close. try this
#include <stdio.h>
int main()
{
double pi = 3.1415926;
double z = 0.0;
printf("What is the radius of the circle? \n ");
scanf("%1f", &z);
double c = 2.0 * pi * z;
printf("The circumference is %.1f", c);
return 0;
}
your logic is telling the float that it needs a whole number, but no decimals afterwards
While analyzing can't seem to avoid the value stored to 'delta' not being read... what part of my loop isn't working and why?
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c;
float delta;
printf("a=");
scanf("%f", &a);
printf("b=");
scanf("%f", &b);
printf("c=");
scanf("%f", &c);
float x, x1, x2;
delta=((b*b)-(4*a*c));
if("delta==0")
{
x=((-b)/(2*a));
printf("x=%f \n", x);
}
else if("delta>0")
{
x1=((-b+sqrt(delta))/(2*a));
x2=((-b-sqrt(delta))/(2*a));
printf("x1=%f i x2=%f \n", x1, x2);
}
else printf("Nie ma w zbiorze liczb rzeczywistych rozwiazania tego rownania.\n");
return 0;
}
1st thing, if("delta==0") inside "" whatever is present that will be treated as a address so if(address) means true, remove the double quotation.
if(delta==0)
// some code
else if (delta > 0)
// some code
2nd thing, you are comparing(==) a float & an integer, so be aware of behaviour of comparing different operands.
I have written the following code to compute the roots of a quadratic equation:
int quadroots(int *ap, int *bp, int *cp,int *root1p, int *root2p, int *dp);
int main (void) {
int ap,bp,cp;
int dp, root1p, root2p;
quadroots(&ap, &bp, &cp, &root1p, &root2p, &dp);
printf("The solutions are: %f, %f", root1p, root2p);
}
int quadroots(int *ap, int *bp, int *cp,int *root1p, int *root2p, int *dp){
int a, b, c, d, root1, root2;
printf("Enter a, b, c \n");
scanf("%d, %d, %d", &a, &b, &c);
if (a==0) {
printf ("The system is linear. The roots cannot be computed using this program: a cannot be 0. Please recompile");
return 0;
}
int b_sqared = b*b;
d = b_sqared - (4 * a * c);
if (d<0) {
d=-d;
printf("The roots of this equation are the complex numbers: \n");
printf("%.3f+%.3fi", ((-b) / (2*a)), (sqrt(d) / (2 * a)));
printf(", %.3f%.3fi", (-b / (2*a)), (-sqrt(d) / (2*a)));
}
else if (d==0) {
printf("The root of this equation are real and equal. \n");
root1= (-d / (2*a));
printf("The roots of this equation are: %.3f, %.3f", root1, root1);
}
else {
printf ("The roots of the quadratic equation are real numbers. \n");
root1 = (-b + sqrt(d)) / (2*a);
root2 = (-b - sqrt(d)) / (2*a);
printf("Roots of the quadratic equation are the real numbers %.3f, %.3f", root1,root2);
}
return 0;
*root1p=root1;
*root2p=root2;
}
This is based off a code I had previously written which worked, but back then, I wasn't using functions.
As it is now, it compiles and runs fine (ie. it takes in the numbers and performs a calculation), but the answer it prints out is totally incorrect.
Eg. for the input "1 5 6" (corresponding to the equation x^2 +5x + 6, it should print out " The roots are real numbers.
The roots are the real numbers 6 and 1"
since those are the roots of the equation. However, it doesn't. What is printed are some absurdly huge numbers (Enter a, b, c
1 5 6
The roots of this equation are the complex numbers:
-2719010580126301300000000000.000+0.000i, -2719010580126301300000000000.0000.000iThe solutions are: 0.000000, 0.000000)
Any help would be much appreciated.
Thank you very much! Best.
printf("%.3f+%.3fi", ((-b) / (2*a)), (sqrt(d) / (2 * a)));
You are using integer division in ((-b) / (2*a)) So you will get incorrect values for some numbers.
You can use.
printf("%.3f+%.3fi", ((-b) / (2.0*a)), (sqrt(d) / (2 * a)));
to force a conversion to a double before the division. You need to do this for all division between two integers in the code.
The design of this programm is horrific, but if you
add #include <math.h> (so sqrt is known)
replace all ints by floats (you don't want integer maths here)
replace scanf("%f, %f, %f", &a, &b, &c); by scanf("%f %f %f", &a, &b, &c); (correct format string for scanf).
it should work more or less.
I didn't dig further, so there may be other problems though.
Hello guys I started learning C few weeks ago and I am trying to make my first useful program, I am trying to create a squared equation calculator but no matter what I type in the input it always outputs "no solution2" can anyone take a look and help me ?
examples for input :
0 0 0=
1 4 1=
#include <stdio.h>
#include <math.h>
int main()
{
printf("enter a\n");
double a; scanf("%1f", &a);
printf("\nenter b\n");
double b; scanf("%1f", &b);
printf("\nenter c\n");
double c; scanf("%1f", &c);
if (a==0)
{
if (b==0)
{
if (c==0)
printf("x can be every number\n");
else
printf("no solution1\n");
}
else
{
printf("x equals %.2f\n", ((-1)*c) / b);
}
}
else
{
double delta = (b*b)-(4*(a*c));
if (delta>0)
{
double sqrtDlt = sqrt(delta);
printf("x1 = %4.2f\n", (((-1)*b) + sqrtDlt) / 2 * a);
printf("x2 = %4.2f\n", (((-1)*b) - sqrtDlt) / 2 * a);
}
else
printf("no solution2\n");
}
return 0;
}
For double use specifier %l(ell)f not %1(one)f in scanf.
Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double.
I have an assignment to compute the roots of quadratic equations in C, should be pretty simple and I know what I need to do with the program but I am having a problem nonetheless.
It works fine when the roots are imaginary and when the term inside the square root is zero.
But when I enter coefficients a, b and c which would give real roots it gives me the wrong answer, and I cant figure out whats wrong. (I am testing it with a=2, b = -5 and c=1)
This is my code, it compiles and runs, but gives the wrong answer.
#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, D, x, x1, x2, y, xi;
printf("Please enter a:\n");
scanf("%f", &a);
printf("Please enter b:\n");
scanf("%f",&b);
printf("Please enter c:\n");
scanf("%f", &c);
printf("The numbers you entered are: a = %f, b = %f, c = %f\n", a, b, c);
D = b*b-4.0*a*c;
printf("D = %f\n", D);
if(D > 0){
x1 = (-b + sqrt(D))/2*a;
x2 = ((-b) - sqrt(D))/2*a;
printf("The two real roots are x1=%fl and x2 = %fl\n", x1, x2);
}
if(D == 0){
x = (-b)/(2*a);
printf("There are two identical roots to this equation, the value of which is: %fl\n", x);
}
if (D<0){
y = sqrt(fabs(D))/(2*a);
xi = (-b)/(2*a);
printf("This equation has imaginary roots which are %fl +/- %fli, where i is the square root of -1.\n", xi, y);
}
return 0;
}
You don't calculate the result correctly:
x = y / 2*a
is actually parsed as
x = (y / 2) * a
so you have to put parentheses around 2*a.
you want this:
x = y / (2 * a)