C program to find the roots of a quadratic equation - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Saturday, June 15, 2019

C program to find the roots of a quadratic equation

In this tutorial we will write a C program that find the roots of a quadratic equation.
Firstly in this C program we will enter the value of a,b and c then we will find the roots of a quadratic equation.

Solution

  • In this program we will use <math.h> header file.
  • In this program we will use six variables a,b,c,root1,root2 and x.
  • We declared these variables a,b and c in int data type and three root1,root2 and x in double data type.
  • Then we will enter the value of a,b and c.
  • We use three logics for find the roots of a quardratic equation. Which are as...   
     x=(b*b)-(4*a*c);
root1=-b+sqrt(x)/(2*a);
root2=-b-sqrt(x)/(2*a); 
  • When we use these statement the value of these operations will be store in own variable.
  • Then we will print the value of root1 and root2.


C program code


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
double root1,root2,x;
clrscr();

printf("\n Enter the value of a=");
scanf("%d",&a);

printf("\n Enter the value of b=");
scanf("%d",&b);

printf("\n Enter the value of c=");
scanf("%d",&c);

x=(b*b)-(4*a*c);
root1=-b+sqrt(x)/(2*a);
root2=-b-sqrt(x)/(2*a);

printf("\n The value of root1 is=%lf",root1);

printf("\n The value of root2 is=%lf",root2);

getch();
}


Output 1:






Output 2:





we will use <math.h> header file in this program because we used sqrt in this program.And  we will use six variables a,b,c,root1,root2 and x.In x variable we defined one statement as

x=(b*b)-(4*a*c);  because this method easy for sqrt.







No comments:

Post a Comment