Jump to content

Recommended Posts

Posted

Calcular e devolver as raízes de uma equação do 2º grau (ax^2+bx+c=0).

#define _CRT_SECURE_NO_DEPRECATE /* nao mostrar erros de scanf e variantes */
#include <stdio.h>
#include <math.h> /* Para usar o sqrt – biblioteca standard  */

/*8.1 - BinDisc - calcula [b^2-4ac] -  */
int BinDisc(int a, int b, int c){
   return ( b*b - 4*a*c );
}

/*8.2 - TemRaizesReais */ 
int TemRaizesReais (int a, int b, int c){
   int bd=BinDisc(a, b, c);
   if (bd==0)
      return 1;
   else 
       if (bd < 0)
           return 0;
       else
           return 2;
}

/*8.3 - CalculaRaizes */
void CalculaRaizes(int a, int b, int c){
   int raizes = TemRaizesReais (a, b, c);
   if (raizes == 1 )
       printf ("raiz dupla x=%.2f \n ",
           b/((float)2*a));
   else if (raizes==2)
       printf (" 2 raizes x1=%.2f e x2=%.2f \n",
           b + sqrt(BinDisc(a, b, c))/((float)2*a),
           b - sqrt(BinDisc(a, b, c))/((float)2*a));
   else printf ("Raizes imaginárias");
}

int main () {
   int a, b, c ;
   printf("Indicar 3 inteiros: ");
   scanf ("%d%d%d",&a ,&b, &c );
   CalculaRaizes(a ,b ,c);
   return 0;
}
  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.