Learn C ProgrammingLearn ProgrammingPrograms On Decision Control StructuresResources

022. Write a program to calculate roots of a quadratic equations in C language

By September 18, 2012 February 5th, 2018 3 Comments

x=b^2-4ac
if x=0 -> only one root ,
if x>0 -> roots are distinct (–b+x)/2a & (–b-x)/2a
if x<0 -> roots are imaginary

#include<stdio.h>
#include<conio.h>  
main()
{
 float x,r1,r2,a,b,c;
 clrscr();
 printf("Enter a,b,c...");
 scanf("%f%f%f",&a,&b,&c);
 x=b*b-4*a*c;
 r1=(-b+x)/2*a;
 r2=(-b-x)/2*a;
 if(x>0)
		printf("\nRoots are unequal...\n");
 else if(x<0)
		printf("\nRoots are imaginary...\n");
 else
		printf("\nRoots are same....\n");
 printf("R1 = %f",r1);
 printf("R2 = %f",r2);
 getch();
}



3 Comments

  • Harsha says:

    In print we can’t write capital R1 as we declaed it as small r1

  • Rouen Pro says:

    //
    // main.c
    // solo008
    //
    // Created by Sann Chamrouen on 1/29/18.
    // Copyright © 2018 Sann Chamrouen. All rights reserved.
    //

    #include
    #include

    int main(int argc, const char * argv[]) {
    float a,b,c,x,r1,r2;
    printf(“Enter the value of a = “);
    scanf(“%f”,&a);
    printf(“Enter the value of b = “);
    scanf(“%f”,&b);
    printf(“Enter the value of c = “);
    scanf(“%f”,&c);
    x=b*b-4*a*c;
    r1=(-b+sqrt(x))/2*a;
    r2=(-b-sqrt(x))/2*a;
    if (x>0) {
    printf(“Roots are unequal \n”);
    }
    else if(x<0)
    printf("Roots are imaginary \n");
    else
    printf("Roots are equal\n");
    printf("Root 1 is = %f\n",r1);
    printf("Root 2 is = %f\n",r2);

    }

Leave a Reply

DEMO01