Fortran Programming and Problem Solving Techniques
1. Problem Solving Techniques Using Computers
Problem-solving using computers involves designing step-by-step solutions to problems. These
solutions are often structured using flowcharts, algorithms, and pseudocode.
1.1 Flowcharts
A flowchart is a diagrammatic representation using symbols to denote different steps. Symbols
include:
- Terminator (Start/End) - Oval
- Process - Rectangle
- Input/Output - Parallelogram
- Decision - Diamond
Example: To find the largest of two numbers, draw arrows representing the sequence of decisions
and outputs.
1.2 Algorithms
An algorithm is a finite sequence of well-defined steps.
Example: Algorithm to compute the factorial of a number N:
Step 1: Start
Step 2: Input N
Step 3: Set F = 1, i = 1
Step 4: While i <= N, F = F * i, i = i + 1
Step 5: Output F
Step 6: End
Fortran Programming and Problem Solving Techniques
1.3 Pseudocode
Pseudocode blends natural language and programming-like syntax.
Example:
IF N mod 2 == 0 THEN
PRINT "Even"
ELSE
PRINT "Odd"
ENDIF
2. Programming in Fortran
Fortran (Formula Translation) is a high-level language used in scientific computing.
2.1 Syntax and Semantics
Every program starts with PROGRAM and ends with END PROGRAM. Variables are declared
explicitly or implicitly.
Example:
PROGRAM Add
INTEGER :: A, B, SUM
A = 10
B=5
SUM = A + B
PRINT *, 'SUM =', SUM
END PROGRAM Add
2.2 Data Types and Structures
Fortran Programming and Problem Solving Techniques
- INTEGER: whole numbers
- REAL: decimal numbers
- DOUBLE PRECISION: high-precision real
- CHARACTER: text
- LOGICAL: boolean
2.3 Input/Output
READ is used to take input; PRINT/WRITE is used for output.
Example:
READ *, A
PRINT *, 'Value:', A
2.4 Loops and Decision Statements
DO and IF statements control program flow.
Example:
DO I = 1, 5
PRINT *, I
END DO
IF (A > B) THEN
PRINT *, 'A is greater'
ELSE
PRINT *, 'B is greater'
ENDIF
Fortran Programming and Problem Solving Techniques
2.5 Arrays
Arrays store multiple values.
Example:
INTEGER :: A(3)
A = (/ 1, 2, 3 /)
2.6 Functions and Subprograms
Functions return values; subroutines perform tasks.
Function:
REAL FUNCTION SQUARE(X)
SQUARE = X * X
END FUNCTION
Subroutine:
SUBROUTINE SWAP(A, B)
INTEGER :: TEMP
TEMP = A
A=B
B = TEMP
END SUBROUTINE
2.7 Recursion
Recursive function to calculate factorial:
RECURSIVE FUNCTION FACT(N) RESULT(F)
IF (N <= 1) THEN
Fortran Programming and Problem Solving Techniques
F=1
ELSE
F = N * FACT(N-1)
ENDIF
END FUNCTION
3. Computing Using Fortran
3.1 Solving Mathematical Problems
Example: Solve quadratic equation
PROGRAM QUADRATIC
REAL :: A, B, C, D, R1, R2
READ *, A, B, C
D = B**2 - 4*A*C
IF (D >= 0) THEN
R1 = (-B + SQRT(D))/(2*A)
R2 = (-B - SQRT(D))/(2*A)
PRINT *, R1, R2
ELSE
PRINT *, 'No real roots'
ENDIF
END PROGRAM
3.2 Scientific Computations
Example: Sine using Taylor Series:
PROGRAM SINE_TAYLOR
Fortran Programming and Problem Solving Techniques
REAL :: X, TERM, SUM
INTEGER :: I
SUM = 0
DO I = 0, 5
TERM = ((-1)**I * X**(2*I+1)) / FACT(2*I+1)
SUM = SUM + TERM
END DO
PRINT *, 'Sine =', SUM
END PROGRAM