CHAPTER TWO
FORTRAN SYNTAX AND SIMPLE PROGRAMMING EXAMPLES
Course Code: ENG 411
Course Title: Computer Programming for Engineers
Credit Units: 2 Units
Level: 400 Level (BEng Engineering)
Semester: First Semester, 2025/2026 Academic Session
2.0 Chapter Overview
This chapter introduces the basic syntax rules of the Fortran programming language and
demonstrates their application through simple programming examples. Emphasis is placed on
correct program structure, syntax conventions, and the writing of clear and error-free Fortran
codes suitable for engineering computations.
2.1 Learning Objectives
At the end of this chapter, students should be able to:
1. Understand the general syntax rules of Fortran.
2. Write properly structured Fortran programs using free-form source code.
3. Declare and use variables correctly.
4. Perform simple input and output operations.
5. Write and execute basic Fortran programs for engineering calculations.
2.2 General Syntax Rules of Fortran
Fortran follows a set of syntax rules that govern how programs are written and interpreted by the
compiler. Key rules include:
• Fortran is case-insensitive (e.g., REAL and real are equivalent).
• Statements are written in free-form style in modern Fortran (Fortran 90 and above).
• Comments are introduced using the exclamation mark !.
• Each program must have a clear beginning and termination.
• Keywords must be spelled correctly and used in proper order.
2.3 Structure of a Fortran Program
A basic Fortran program consists of the following components:
1. Program statement
2. Specification section (variable declarations)
3. Execution section
4. End program statement
General Program Template
program program_name
implicit none
! Variable declarations
! Executable statements
end program program_name
2.4 Comments in Fortran
Comments are used to explain program logic and improve readability. In Fortran, comments
begin with the ! symbol.
Example:
! This program computes the area of a circle
2.5 Variable Declaration and Assignment
All variables must be declared before use when implicit none is specified.
Example of Variable Declaration
integer :: count
real :: radius, area
Assignment Statement
radius = 5.0
area = 3.142 * radius * radius
2.6 Input and Output Statements
Fortran provides simple commands for input and output operations.
Output Statement
print *, "Area = ", area
Input Statement
read *, radius
2.7 Simple Fortran Examples
Example 2.1: Displaying a Message
program welcome
implicit none
print *, "Welcome to Fortran Programming for Engineers"
end program welcome
Example 2.2: Simple Arithmetic Operation
This program calculates the area of a circle.
program circle_area
implicit none
real :: radius, area
print *, "Enter the radius of the circle:"
read *, radius
area = 3.142 * radius * radius
print *, "The area of the circle is = ", area
end program circle_area
Example 2.3: Temperature Conversion
This program converts temperature from Celsius to Fahrenheit.
program temperature conversion
implicit none
real :: celsius, fahrenheit
print *, "Enter temperature in Celsius:"
read *, celsius
fahrenheit = (9.0/5.0) * celsius + 32.0
print *, "Temperature in Fahrenheit = ", fahrenheit
end program temperature_conversion
Example 2.4: Area of a Circular Plate (Mechanical Engineering)
Problem Statement:
A circular steel plate has a radius of 0.75 m. Write a Fortran program to compute the area of the
plate.
Mathematical Model:
𝐴 = 𝜋𝑟 2
Fortran Program:
program circular_plate
implicit none
real :: radius, area
radius = 0.75
area = 3.142 * radius * radius
print *, "Area of circular plate = ", area, " m^2"
end program circular_plate
Engineering Interpretation:
The computed area is required in stress analysis and heat-transfer calculations involving flat
plates.
Example 2.4: Ohm’s Law (Electrical Engineering)
Problem Statement:
An electrical circuit has a voltage of 240 V and a resistance of 60 Ω. Determine the current
flowing in the circuit using Fortran.
Mathematical Model:
𝑉
𝐼=
𝑅
Fortran Program:
program ohms_law
implicit none
real :: voltage, resistance, current
voltage = 240.0
resistance = 60.0
current = voltage / resistance
print *, "Circuit current = ", current, " A"
end program ohms_law
Engineering Interpretation:
The current obtained is useful in sizing conductors and protective devices.
Example 2.5: Average Acceleration (Civil/Mechanical Engineering)
Problem Statement:
A vehicle accelerates uniformly from rest to a velocity of 30 m/s in 12 seconds. Compute the
average acceleration.
Mathematical Model:
𝑣−𝑢
𝑎=
𝑡
Fortran Program:
program acceleration
implicit none
real :: u, v, t, a
u = 0.0
v = 30.0
t = 12.0
a = (v - u) / t
print *, "Average acceleration = ", a, " m/s^2"
end program acceleration
Engineering Interpretation:
This result is relevant in transport engineering and motion analysis.
Example 2.6: Simple Interest (General Engineering Computation)
Problem Statement:
Calculate the simple interest on a principal of ₦150,000 at an annual rate of 8% over 3 years.
Mathematical Model:
𝑆𝐼 = 𝑃𝑅𝑇
Fortran Program:
program simple_interest
implicit none
real :: P, R, T, SI
P = 150000.0
R = 0.08
T = 3.0
SI = P * R * T
print *, "Simple Interest = ₦", SI
end program simple_interest
Engineering Interpretation:
Demonstrates correct variable declaration, arithmetic operations, and formatted output.
2.8 Common Syntax Errors
Some common errors encountered when writing Fortran programs include:
• Missing implicit none
• Undeclared variables
• Mismatched program and end program names
• Incorrect use of quotes in output statements
• Typographical errors in keywords
2.9 Summary
In this chapter, the basic syntax of the Fortran programming language has been presented.
Students have learned how to structure a Fortran program, declare variables, use input and output
statements, and write simple programs for engineering applications. These concepts form the
foundation for more advanced programming constructs in subsequent chapters.
Tutorial Questions
1. State five general syntax rules of Fortran.
2. Explain the purpose of implicit none in a Fortran program.
3. Write a Fortran program to compute the volume of a cylinder.
4. Differentiate between input and output statements with examples.
5. Identify and correct errors in a given Fortran code snippet.
Practical Assignment
1. Write a Fortran program to calculate the simple interest given principal, rate, and time.
2. Modify the program to display appropriate messages for user input and output.
3. Compile and execute the program, and submit the source code and output.