I'm doing a course in C,where i was asked to find the roots of a quadratic equation.First i tried by hard-coding and it worked. Next i gave inputs using scanf(like a,b,c) it worked. But i failed in a scenario where the whole quadratic expression is taken as input i.e (ax^2+bx+c) and retrieve these a,b,c values from the expression. I spent a lot of time on this,searched online i couldn't find the answer so i am asking here for help.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.14
int main(void)
{
puts("---- ROOTS ----");
char equ[20]; //Quadratic Expression in an Array
float a,b,c,ope;
float root1,root2;
printf("please provide the expression :");
scanf("%d",&equ[20]);//Example : 5x^2+3x+1 as input
a == equ[0];//since ax^2+bx+c in the above expression a==5
b == equ[3];//b==3
c == equ[6];//c==1
ope = sqrt(b*b -4*a*c);
root1 = (-b + ope)/2*a;
root2 = (-b - ope)/2*a;
printf("The root 1 of the expression is : %d", root1);
printf("\nThe root 2 of the expression is : %d", root2);
return EXIT_SUCCESS;
}
OUTPUT :
PS F:\Thousand C GO> gcc 3.c
PS F:\Thousand C GO> ./a
---- ROOTS ----
please provide the expression :5x^2+3x+1//edited
The root 1 of the expression is : 0
The root 2 of the expression is : 0
I wanted to know if there is a way to solve this problem in C,if so how? if not why?.
Help is much appreciated. Thanks.