Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

THIS A CODE TO PRINT ROOTS OF QUADRATIC EQN USING FUNCTIONS ... NOT WORKING!!

+6 votes
asked Jan 24, 2023 by Naman Jain (180 points)
#include <math.h>

#include <stdio.h>

int

main ()

{

  int a, b, c;

  float D;

  int x;

  printf ("enter the co-effecients of the equation : ");

  scanf ("%d %d %d", &a, &b, &c);

  D=ROOTS(a,b,c);

  printf("%f",D);

   if (D > 0)

    {

      printf ("\nthe roots are distinct and real \nroot 1 %.0f\nroot 2 %.0f",

      (-b + D) / (2 * a), (-b - D) / (2 * a));

    }

  else if (D == 0)

    {

      printf ("\nthe roots are real and equal \nroot 1 %.0f\nroot 2 %.0f",

      -b / (2 * a), -b / (2 * a));

    }

  else if (D < 0)

    {

      printf ("\nno real roots");

    }

    return 0;

}

int ROOTS(int l,int  m, int n)

{

    double E;   

    E = sqrt ((m * m) - (4 * l * n));

  return(E);

}

1 Answer

+1 vote
answered Jan 25, 2023 by Peter Minarik (86,040 points)

You are using various types all around: int, float, double.

For instance, in your ROOTS function you force the value to be returned as an integral number, losing all the decimal values.

When you print out the results, you tell the compiler to not print any decimal digits.

Your code fixed is below:

#include <math.h>
#include <stdio.h>

float ROOTS(int l, int m, int n)
{
    return sqrt ((m * m) - (4 * l * n));
}

int main ()
{
    int a, b, c;
    printf("enter the co-effecients of the equation : ");
    scanf("%d %d %d", &a, &b, &c);
    float D = ROOTS(a, b, c);
    printf("D: %f", D);
    if (D > 0)
    {
        printf("\nthe roots are distinct and real \nroot 1 %f\nroot 2 %f", (-b + D) / (2.0f * a), (-b - D) / (2.0f * a));
    }
    else if (D == 0)
    {
        printf("\nthe roots are real and equal \nroot 1 %f\nroot 2 %f", -b / (2.0f * a), -b / (2.0f * a));
    }
    else if (D < 0)
    {
        printf ("\nno real roots");
    }
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...