Challenge.1_C Program To Find Sum and Product of roots

     Hey Guys! This is the solution post for Challenge.1_C Program to find sum and product of roots. 

C program to find sum and product of roots

 ax^2 + bx + c = 0

  • LOGIC:

Since we know that sum of the roots is : x1 + x2 = -b/a  and product of roots is : x1*x2 = c/a. So we'll first take the input of coefficients from user and calculate sum and product and print it.

  • ALGORITHM:
  1. Start
  2. Input values of a, b and c from user
  3. Calculate sum of roots : -b/a
  4. Calculate product of roots : c/a
  5. Print sum of roots : x1+x2 = sum
  6. Print product of roots : x1*x2 = prod
  7. End
  • CODE: 
#include <stdio.h>

int main ()
{
  //declare variables
  float a,b,c,sum,prod;
  printf("Enter the values of a, b and c : ");
  //input the values of a, b and c from user
  scanf("%f %f %f", &a, &b, &c);

  //calculate sum of roots
  sum = -b/a;
  //calculate product of roots
  prod = c/a;

  //print sum and product of roots
  printf("Sum of the roots is : %.2f",sum);
  printf("\n Product of the roots is : %.2f",prod);

  return 0;
}
 
  •  OUTPUT:

Enter the values of a, b and c : 1 4 4
Sum of the roots is : -4.00
Product of the roots is : 4.00
 

 

That's all for today. If you get any problem then don't hesitate to ask.

If the post was helpful, then 

Share, Subscribe and keep Coding...

Comments