Anda di halaman 1dari 1

FORTRAN

CONTOH LATIHAN (PENGANTAR)


* PROGRAM PERSAMAAN KUADRAT
* ------------------------C
WRITE (*,'(1X,A)') 'AKAR PERSAMAAN KUADRAT :'
WRITE (*,'(1X,A)') ' AX^2 + BX + C = 0 '
WRITE (*,*)
1
WRITE (*,'(1X,A,\)') ' NILAI KOEFISIEN A ? '
READ (*,'(F9.3)') A
C
C SELEKSI NILAI A TIDAK BOLEH SAMA DENGAN NOL
IF (A.EQ.0.0) GOTO 1
C
WRITE (*,'(1X,A,\)') ' NILAI KOEFISIEN B ? '
READ (*,'(F9.3)') B
WRITE (*,'(1X,A,\)') ' NILAI KOEFISIEN C ? '
READ (*,'(F9.3)') C
WRITE (*,*)
C
C HITUNG NILAI DARI DETERMINAN
D = B**2 - 4*A*C
C
C SELEKSI NILAI DETERMINAN
C
IF (D.LT.0.)THEN
*
NILAI DETERMINAN NEGATIF, BERARTI ADA DUA AKAR BIL KOMPLEK BERLAINAN
Z1 = (-B)/(2*A)
Z2 = SQRT(-D)/(2*A)
WRITE (*,*) 'ADA DUA AKAR BIL KOMPLEK BERLAINAN :'
WRITE (*,*) '________________________________'
WRITE (*,'(1X,A,F9.3,A,F9.3,A)') 'X1= ',Z1,'+', Z2,'i'
WRITE (*,'(1X,A,F9.3,A,F9.3,A)') 'X1= ',Z1,'-', Z2,'i'
ENDIF
C
IF (D.EQ.0.) THEN
*
NILAI DETERMINAN NOL, BERARTI ADA 2 AKAR REAL KEMBAR
X1 = (-B)/(2*A)
WRITE (*,*) 'ADA DUA AKAR REAL KEMBAR :'
WRITE (*,*) '__________________________'
WRITE (*,'(1X,A,F9.3)')'X1=X2=',X1
ENDIF
C
IF (D.GT.0.) THEN
*
NILAI DETERMINAN POSITIF, BERARTI ADA 2 AKAR REAL BERLAINAN
X1 = (-B + SQRT(D))/(2*A)
X2 = (-B - SQRT(D))/(2*A)
WRITE (*,*) 'ADA DUA AKAR REAL BERLAINAN :'
WRITE (*,*) '_____________________________'
WRITE (*,'(1X,A,F9.3)') 'X1= ',X1
WRITE (*,'(1X,A,F9.3)') 'X2= ',X2
ENDIF
C
C SELESAI
END