Equazione 1° grado

Risolvere un’equazione di primo grado ax+b=0, dati i valori dei coefficienti a e b.

primo_gradoAnalisi

Un po’ di matematica…

eq1

Esempi

Istanza Elaborazione Risposta
1 x+2=0 a=1
b=2
x=-2/1 -2
2 2x-3=0 a=2
b=-3
x=-(-3)/2 3/2
3 0x+0=0 a=0
b=0
0=0 Indeterminata
4 0x+2=0 a=0
b=2
2=0 Impossibile
Program PrimoGrado;
Var
    a, b: Real;
    x: Real;
begin
    Write('a? '); Readln(a);
    Write('b? '); Readln(b);

    If(a <> 0) Then
        Begin
            x:=-b/a;
            Writeln(x:1:2);
        End
    Else
        Begin
            If(b = 0) Then
                Begin
                    Writeln('INDETERMINATA')
                End
            Else
                Begin
                    Writeln('IMPOSSIBILE');
                End;
        End;

    ReadLn;
End.

Più compatto…

...
   If(a <> 0) Then
      Begin
         x:=-b/a;
         Writeln(x:1:2);
      End
   Else If(b = 0) Then
      Writeln('INDETERMINATA')
   Else
      Writeln('IMPOSSIBILE');
...