Category Archives: Mathematics

Mathematics : Roots Of A Quadratic Equation With Degree 2

/*====Quadratic Equations With Degree 2====*/
/*====Only Real Roots (Not Imaginary)====*/

#include<math.h>
#include<stdio.h>
#include<conio.h>
int main()
{
float a,b,c,x1,x2,x3;
int flag=0;
clrscr();
printf("\n====Quadratic Equations (without imaginary roots)====\n\n");
printf("Enter the value of a=\n");
scanf("%f",&a);
printf("Enter the value of b=\n");
scanf("%f",&b);
printf("Enter the value of c=\n");
scanf("%f",&c);
printf("The quadratic equation is : (%f)*x*x + (%f)*x + (%f)\n",a,b,c);
if(a==0 && b==0)
{
printf("Not a valid equation!\n");
flag=1;
}
if(a==0 && b!=0)
{
x1=-(c/b);
printf("The only root is : %f",x1);
flag=1;
}
if(flag==0)
{
if((b*b-4*a*c)>=0)
{
x2=(b*b)-(4*a*c);
x1=(-b+sqrt(x2))/(2*a);
x3=(-b-sqrt(x2))/(2*a);
printf("root1 : %f",x1);
printf("\nroot2 : %f",x3);
}
if((b*b-4*a*c)<0)
{
printf("Not a real root,\ni.e. the root contains imaginary part.");
}
}
getch();
return 0;
}

Output :

Untitled

That’s All. P)
Happy C-ing!

-Aayush Shrivastava


Are you satisfied?


 

Mathematics : Bisection Method Using C Language

/*===Bisection Method===*/
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
void main()
{
int i=1;
float f,x,x0,x1,m;
clrscr();
printf("=====Bisection Method=====");
printf("Enter the two values,i.e. the range\nbetween which the root of equation is lying:\n");
scanf("%f %f",&x0,&x1);
printf("\n|Iteration i | x0 | x1 | f(Xi) |\n");
printf("|____________|__________________|
__________________|_____________________|\n");
do
{
x = (x0+x1)/2;
f = (2*x*x*x-8*x*x+1); //Change the equation according to the question
if(f>0)
{
x1=x;
}
else
{
x0=x;
}
printf("| %d\t | %f\t| %f\t | %f\t |\n",i,x0,x1,f);
i++;
delay(100);
}
while((x1-x0)>0.0001);
printf("\n\t The Root Of The Equation Is %f",x);
getch();
}

Output :

Untitled

That’s All. P)
Happy C-ing!

-Aayush Shrivastava


Are you satisfied?