노트/Fortran 연습
Program 3.1. 이차 방정식
Jae-seong Yoo
2014. 4. 6. 18:29
PROGRAM QuadraticEquations_1 !--------------------------------------------------------------------------------- ! Program to solve a quadratic equation using the quadratic formula. ! ! Variables used are: ! A, B, C : the coefficients of the quadratic equation ! Discriminant : the discriminant, B**2 - 4.0*A*C ! Root_1, Root_2 : the two roots of the equation ! ! Input : The coefficients A, B, and C ! Output : The two roots of the equation or the (negative) discriminant ! and a message indicating that there are no real roots !--------------------------------------------------------------------------------- IMPLICIT NONE REAL A, B, C, Discriminant, Root_1, Root_2 ! Get he coefficients PRINT *, "Enter the coefficients of the quadratic equation:" READ *, A, B, C ! Calculate the coefficients Discriminant = B**2 - 4.0*A*C ! Check if discriminant is nonnegative. If it is, calculate and ! display the roots. Otherwise display the value of the discriminant ! and a no-real-roots message. IF (Discriminant >= 0) THEN Discriminant = SQRT(Discriminant) Root_1 = (-B + Discriminant) / (2.0 * A) Root_2 = (-B - Discriminant) / (2.0 * A) PRINT *, "The roots are", Root_1, Root_2 ELSE PRINT *, "Discriminant is", Discriminant PRINT *, "There are no real roots" END IF pause End PROGRAM QuadraticEquations_1