PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on If & Else

Program to find all roots of the quadratic equation

Input - Output

Input
Enter coffiecnt of x^2,x and consonant : 1 1 -6

Output
roots are real and distinct

root1 = 2 and root2 = -3

Algorithm

Step 1: START

Step 2: Enter value of coffiecnt of X^2,X,and consonant.

Step 3: Calculate D=(b*b)-(4*a*c)

    if D==0

       print roots are equal;

    else if (D>0)

       print roots are real and distinct;

    else

       print roots are imagenary;

Step 4: END

CODE


/* find roots of quadratic equation */

#include<stdio.h>
#include<math.h>
int main()
{
      int a,b,c,root1,root2,D;
/* Quadratic equation ax2+bx+c */
      printf("Enter the value of a , b , c : ");
      scanf("%d%d%d",&a,&b,&c);
      D = (b*b)-(4*a*c);
      if(D == 0)
    {
        printf(" roots are real and equal \n");
        root1=root2=(-b)/(2*a);
        printf(" root1 = %d and root2 = %d ",root1,root2);
    }
      else if(D > 0)
    {
        printf(" roots are real and distinct \n");
        root1=((-b)+sqrt(D))/2*a;
        root2=((-b)-sqrt(D))/2*a;
        printf(" root1 = %d and root2 = %d ",root1,root2);
    }
      else
        printf(" roots are imagenary \n");
    return 0;
}


/* Find roots of quadratic equation */

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
      int a,b,c,root1,root2,D;
/* Quadratic equation ax2+bx+c */
      cout<<"Enter the value of a , b , c : ";
      cin>>a>>b>>c;
      D = (b*b)-(4*a*c);
      if(D == 0)
    {
        cout<<" roots are real and equal \n";
        root1=root2=(-b)/(2*a);
        cout<<" root1 = "<<root1<<" and root2 = "<<root2;
    }
      else if(D > 0)
    {
        cout<<" roots are real and distinct \n";
        root1=((-b)+sqrt(D))/2*a;
        root2=((-b)-sqrt(D))/2*a;
        cout<<" root1 = "<<root1<<" and root2 = "<<root2;
    }
      else
        cout<<" roots are imagenary ";
    return 0;
}

OutPut

Enter coffiecnt of x^2,x and consonant : 1 1 -6

roots are real and distinct
root1 = 2 and root2 = -3