C program to find the all roots of a quardratic equation-using if statement | My CS Tutorial - My CS Tutorial

Breaking

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

Tuesday, June 25, 2019

C program to find the all roots of a quardratic equation-using if statement | My CS Tutorial

In this tutorial we will write a C program that find the roots of a quadratic equation by using if statement when the value of (b*b)-(4*a*c) is must be greater than zero.
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.
  • We will use if statement after the x variable's operations as x=(b*b)-(4*a*c);
  • 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);
if(x!=0)
{
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. And after it we use if statement.


I hope that you understand everything easily.If you have any problem or question so you write a comment and follow me on this blog and my Facebook page and on Instagram.Share the post.

No comments:

Post a Comment