C program to find the roots of a quadratic equation

Q. Write a c program to find the roots of a quadratic equation.

Q. Write a c program to solve quadratic equation.

SOLUTION :

#include<stdio.h>
# include <conio.h>
#include<math.h>
void main(){
  float a,b,c;
  float d,root1,root2;
  printf("\n Enter the values of A : ") ;
  scanf("%f", &a) ;
  printf("\n Enter the values of B : ") ;
  scanf("%f", &b) ;
  printf("\n Enter the values of C : ") ;
  scanf("%f", &c) ;

  d = b * b - 4 * a * c;

  if(a != 0){

	  if(d < 0){
		printf("\n RESULT : \n Roots are complex/imaginary number.\n");
		printf(" Roots of quadratic equation are: ");
		printf("\n Root-1 = %.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
		printf("\n Root-2 = %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));
		return 0;
	  }
	  else if(d==0){
	   printf("\n RESULT : \n Both roots are real numbers and equal.\n");
	   root1 = -b /(2* a);
	   printf(" Root of quadratic equation is:\n Root-1 = Root-2 = %.3f ",root1);
	   return 0;
	  }
	  else if(d > 0){
	   printf("\n RESULT : \n Roots are real numbers and unequal.\n");
	   root1 = ( -b + sqrt(d)) / (2* a);
	   root2 = ( -b - sqrt(d)) / (2* a);
	   printf(" Roots of quadratic equation are:\n Root-1 = %.3f , Root-2 = %.3f",root1,root2);
	  }
	}
  else
  printf("\n RESULT :\n Equation is linear.");
  getch();
}

By using SWITCH case :

SOLUTION :

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main()
{
    float a, b, c, d, real, imag, root1, root2, n ;
    int k ;
    printf("\n Enter the values of A : ") ;
    scanf("%f", &a) ;
    printf("\n Enter the values of B : ") ;
    scanf("%f", &b) ;
    printf("\n Enter the values of C : ") ;
    scanf("%f", &c) ;

    if(a != 0){
        d = b * b - 4 * a * c ;
        if(d < 0)
        k = 1 ;
        if(d == 0)
        k = 2 ;
        if(d > 0)
        k = 3;

        switch(k){
            case 1 :
            printf("\n RESULT : \n Roots are complex/imaginary number.\n");
			printf(" Roots of quadratic equation are: ");
			printf("\n Root-1 = %.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a));
			printf("\n Root-2 = %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a));
            break ;

            case 2 :
            printf("\n RESULT : \n Both roots are real numbers and equal.\n");
			root1 = -b /(2* a);
			printf(" Root of quadratic equation is:\n Root-1 = Root-2 = %.3f ",root1);
            break ;

            case 3 :
            printf("\n RESULT : \n Roots are real numbers and unequal.\n");
			root1 = ( -b + sqrt(d)) / (2* a);
			root2 = ( -b - sqrt(d)) / (2* a);
			printf(" Roots of quadratic equation are:\n Root-1 = %.3f , Root-2 = %.3f",root1,root2);
            break ;
        }
    }
    else
    printf("\n RESULT :\n Equation is linear.") ;
    getch() ;
}

OUTPUT OF SOLUTION :

complex numbers

linear equation

real numbers

real number unequal

Advertisements

5 responses to “C program to find the roots of a quadratic equation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s