# Equation Solving with C Programming

In theory of equations, the general equations encountered are quadratic equations, cubic equations and n linear equations of n variables. In this article, I am going to discuss how to solve such equations by yourselves and by C programming.

## Equations

An equation is a mathematical notation consisting of an equal to (=) symbol and two expressions on both sides of the equal to (=) symbol. The basic terms used in the theory of equations are as follows.
Variable : Unknown values are replaced with alphabets like x, y etc,. These are what called as variables. Finding the values of such variables is referred to as solving the equation.
Degree : The highest power appearing in the equation is called the degree of the equation. For example, degree of the equation x2+2x+1=0 is 2 because 2 is the maximum power appearing in the equation.
Root : The values of variables present in the equation where the equation holds true are called the roots of the equation. For example, 1 and 3 are the roots of the equation x2-4x+3=0 because (1)2-4(1)+3=0 and (3)2-4(3)+3=0 are true.
Solution : Solutions are same as roots, but without considering repetitions. For example, the roots of the equation x2-2x+1=0 are 1, 1 but solution is 1 (taken only once) i.e. number of roots is two but number of solutions is one.

Some types of equations are as follows.
1) Degree = 1 & Number of Variables = 1 (Referred to as Linear Equation) (Example : 2x+3=0)
2) Degree = 2 & Number of Variables = 1 (Referred to as Quadratic Equation) (Example : 2x2+3x+4=0)
3) Degree = 3 & Number of Variables = 1 (Referred to as Cubic Equation) (Example : 2x3+3x2+4x+5=0)
4) Degree = 4 & Number of Variables = 1 (Referred to as Bi Quadratic Equation) (Example : 2x4+3x3+4x2+5x+6=0)
5) Degree = 1 & Number of Variables = n (Referred to as n Linear Equations of n variables) (Example : 2x+3y+4z=5 & 3x+4y+5z=6 & 4x+5y+6z=7 where n=3)
Here, n is any integer greater than 2 (if n=1, it is nothing but a normal Linear Equation). As solving equations of type (1) is simple (just a matter of division) and solving equations of type (4) is complex (beyond the scope), I am going to discuss how to solve the equations of type (2), type (3) and type (5) only.

The following image shows the method of obtaining the formula for roots of a quadratic equation.

There are 10 lines in the image which can be explained as follows.
Line 1 : Suppose the quadratic equation to solve is ax2+bx+c=0.
Line 2 : Divide both sides of the equation with 'a'.
Line 3 : Multiply numerator and denominator of 'x' coefficient with 2.
Line 4 : Use the algebraic identity a2+2ab=(a+b)2-b2.
Line 5 : Algebraic Simplification.
Line 6 : Algebraic Simplification.
Line 7 : Algebraic Simplification.
Line 8 : Algebraic Simplification.
Line 9 : Apply square root on both sides.
Line 10 : Two possible values of 'x' are obtained which are the roots of the considered quadratic equation.

### Solving Cubic Equations

The solution of a cubic equation comprises of two steps. They are:

Conversion to x3+px+q=0 form
A mathematician named Cardan provided a solution for a cubic equation of the form x3+px+q=0. This method is commonly called Cardan's method. Any cubic equation of the form AX3+BX2+CX+D=0 can be converted into x3+px+q=0 form as shown in the following image.

There are 10 lines in the image which can be explained as follows.
Line 1 : Suppose the cubic equation to solve is AX3+BX2CX+D=0.
Line 2 : Replace X with (x-B/3A) which helps in removing the second degree term.
Line 3 : Use the algebraic identities (a+b)3=a3+b3+3a2b+3ab2 and (a+b)2=a2+b2+2ab.
Line 4 : Algebraic Simplification.
Line 5 : Group terms of same degree.
Line 6 : Group terms of same degree.
Line 7 : Algebraic Simplification.
Line 8 : Divide both sides of the equation with 'A'.
Line 9 : Assume 'p' and 'q' values.
Line 10 : Conversion successful and values 'p' and 'q' are obtained in terms of A, B, C, D.

Solving x3+px+q=0 using Cardan's method
Cardan's method starts with assuming x=u+v and then proceeds as follows.

x3+px+q=0

(u+v)3+p(u+v)+q=0

Using the algebraic identity (a+b)3=a3+b3+3ab(a+b), we get

u3+v3+3uv(u+v)+p(u+v)+q=0

(u3+v3)+(3uv+p)(u+v)+q=0

Let us suppose 3uv+p=0. Substituting this in the above equation, we get

uv=-p/3 & u3+v3=-q

u3v3=-p3/27 and u3+v3=-q

Using the fact that a, b are roots of the quadratic equation x2-(a+b)x+ab=0, u3 and v3 are roots of the quadratic equation x2+qx-p3/27=0. By using the formula for the roots of a quadratic equation mentioned in previous section, we can obtain u3 and v3 in terms of p and q. For every real number, there are three cube roots in which one is real number and the other two are complex numbers. Consider the real number cube root for both u3 and v3. Thus real numbers u, v and x=u+v can be obtained. Therefore, we now know one real root of the transformed cubic equation. Using polynomial division, it is easy to obtain the other two roots no matter they are real or complex. Adding (B/3A) to the obtained roots, we get the roots of the considered cubic equation.

### Solving n Linear Equations of n variables

To solve this type of equations, Cramer's method can used which is as follows.
1) Suppose there are n variables x1, x2, …… xn and n equations E1, E2, …… En.
2) Form a 'n x n' matrix A0 with the n coefficients of Ei as 'i'th row where 'i' varies from 1 to n.
3) Form 'n' other matrices Ai by replacing 'i'th column of A0 with n Right Hand Side terms of the n equations.
4) Calculate determinants of all the matrices A0, A1, …… An and denote them as |A0|, |A1|, ……| An| respectively.
5) Then, the values of x1, x2, …… xn are |A1|/|A0|, |A2|/|A0|, …… |An|/|A0| respectively.

## C Program to solve Equations

This is a menu driven C program which can be used to solve quadratic equations, cubic equations and n linear equations of n variables.

Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
double determinant(double A[][10],double a)
{
double B[10][10],b,I,J,M,N,P,Q;
if(a==1)
b=A[0][0];
else
{
for(P=0,b=0,Q=1;P<a;P++)
{
for(I=0,M=0,N=0;I<a;I++)
{
for(J=0;J<a;J++)
{
B[I][J]=0;
if(I!=0&&J!=P)
{
B[M][N]=A[I][J];
if(N<(a-2))
N++;
else
{
N=0;
M++;
}
}
}
}
b+=Q*A[0][P]*determinant(B,a-1);
Q=-1*Q;
}
}
return(b);
}
void solvincubic(double a,double b,double *c,double *d)
{
double e,f;
e=sqrt(a*a+b*b);
f=(b>=0)?(acos(a/e)):(-acos(a/e));
*c=exp(log(e)/3)*cos(f/3);
*d=exp(log(e)/3)*sin(f/3);
}
void stardisplay(void)
{
double a;
for(a=0;a<77;a++)
printf("*");
printf("\n ");
}
void main(void)
{
double choice;
double a,b,c,d,e,f,g,h,i,j,k,l;
double A[10][10],B[10],C[10][10],D[10];
clrscr();
for(choice=0;choice<=4;choice++)
{
printf("\n 1.(a)pow[X,2]+(b)X+(c)=0 ");
printf(" 2.(a)pow[X,3]+(b)pow[X,2]+(c)X+(d)=0 ");
printf("\n\n 3.Solution of (n) linear equations of (n) variables ");
printf(" 4.Off ");
scanf("%lf",&choice);
if(choice==1)
{
printf("\n Enter {a,b,c} values:");
scanf("%lf,%lf,%lf",&a,&b,&c);
if(a!=0)
{
d=b*b-4*a*c;
if(d>=0)
{
e=(-b+sqrt(d))/(2*a);
f=(-b-sqrt(d))/(2*a);
printf("\n {%lf},{%lf} are roots \n\n ",e,f);
}
else
{
e=-b/(2*a);
f=sqrt(fabs(d))/(2*fabs(a));
printf("\n {%lf+%lfi},{%lf-%lfi} are roots \n\n ",e,f,e,f);
}
printf("of eqn (%lf)pow[X,2]+(%lf)X+(%lf)=0 \n\n ",a,b,c);
}
else
printf("\n IF [a=(%lf)] IT IS A LINEAR EQN:TRY AGAIN \n\n ",a);
}
else if(choice==2)
{
printf("\n Enter {a,b,c,d} values:");
scanf("%lf,%lf,%lf,%lf",&a,&b,&c,&d);
if(a!=0)
{
e=(3*a*c-b*b)/(3*a*a);
f=(2*b*b*b-9*a*b*c+27*a*a*d)/(27*a*a*a);
g=(27*f*f+4*e*e*e)/27;
if(g>=0)
{
h=0.5*(sqrt(g)-f);
i=(h>=0)?(exp(log(fabs(h))/3)):(-exp(log(fabs(h))/3));
j=(3*a*i*i-a*e-b*i)/(3*a*i);
k=-(b+a*j)/(2*a);
l=(sqrt(4*a*(c+b*j+a*j*j)-(b+a*j)*(b+a*j)))/(2*fabs(a));
printf("\n {%lf},{%lf+%lfi},{%lf-%lfi} are roots \n\n ",j,k,l,k,l);
}
else
{
solvincubic(-f/2,0.5*sqrt(fabs(g)),&h,&i);
j=((3*h*h*h+3*h*i*i-e*h)/(3*h*h+3*i*i))-(b/(3*a));
k=(-b-a*j+sqrt((b+a*j)*(b+a*j)-4*a*(c+b*j+a*j*j)))/(2*a);
l=(-b-a*j-sqrt((b+a*j)*(b+a*j)-4*a*(c+b*j+a*j*j)))/(2*a);
printf("\n {%lf},{%lf},{%lf} are roots \n\n ",j,k,l);
}
printf("of eqn (%lf)pow[X,3]+(%lf)pow[X,2]+(%lf)X+(%lf)=0 \n\n ",a,b,c,d);
}
else
printf("\n IF [a=(%lf)] IT IS A QUADRATIC EQN:TRY AGAIN \n\n ",a);
}
else if(choice==3)
{
printf("\n Enter {n} value:");
scanf("%lf",&a);
printf("\n Enter {coefficients} in order [equation wise]:");
for(b=0;b<a;b++)
{
for(c=0;c<a;c++)
scanf("%lf",&A[b][c]);
}
printf("\n Enter {results} [equation wise]:");
for(b=0;b<a;b++)
scanf("%lf",&B[b]);
d=determinant(A,a);
if(d!=0)
{
for(e=0;e<a;e++)
{
for(b=0;b<a;b++)
{
for(c=0;c<a;c++)
C[b][c]=A[b][c];
}
for(b=0;b<a;b++)
C[b][e]=B[b];
D[e]=determinant(C,a)/d;
}
printf("\n Solution is");
for(b=0;b<a;b++)
printf(" {%lf} ",D[b]);
printf("\n\n ");
}
else
printf("\n GIVEN SYSTEM OF EQUATIONS IS INCONSISTENT:TRY AGAIN \n\n ");
}
else if(choice==4)
{
printf("\n ");
stardisplay();
exit(0);
}
else
{
printf("\n {%lf} IS AN INVALID CHOICE:TRY AGAIN \n\n ",choice);
choice1=0;
}
stardisplay();
}
getch();
}

There are four header files stdio.h, conio.h, math.h, stdlib.h in the program for standard input and output, conditional input and output, mathematical and standard library operations respectively. There are three functions in the program other than main function. They are determinant, solvincubic, stardisplay for determinant calculation in type (5) equations, evaluating a part of formula for roots of a cubic equation, printing a line with star (asterisk) symbols for good user interface respectively. Selection of different values of the variable 'choice' enables you to solve different types of equations. The program is written based on the methods mentioned in the previous section with a small exception for cubic equations. I derived formulas for the roots of a cubic equation based on the method mentioned in the previous section and used the formulas to write the corresponding part of the program. This program never gives error outputs. In addition, this program displays error message if coefficient of the term with highest power is given as zero or if an invalid value of 'choice' is given.

Output
Some sample outputs of the above mentioned program are as follows.