FORTRAN


Table of Contents:

Unit 1: Introduction

Unit 2: Program structure

Unit 3: Program Lab Exercises


UNIT 1

INTRODUCTION

Fortran is programming language use for numeric computation and scientific computing. Version of Fortran: fortran 77, 90, 95. But in this tutorial will be coding in Fortran 95.

To run the code, you can use any of the following tools:

Ideas of Fortran

Translator

Compiler

Computer IDE

SilverFrost

Mobile App

DCoder

Online fortran compiler

www.onlinegdb.com

Case sensitivity

Case does not matter

Code termination

It does not terminate

Display content on the screen

print*, ‘dmactutor’

Accept input from the user in the console

read*, age

 

 

UNIT 2

PROGRAM STRUCTURE, COMMENT, VARIABLE AND DATATYPE

2.1    Program Structure

Program structure gives the overview of a program for a particular programming language, and also with emphasis on the individual components of the program and the interrelationships between these components.

program filename

implicit none

//statements to be executed

end program filename

 

2.2    Comment

Comment is used to add additional information to a program by improving the readability of the program. It helps to indicate what needs to be done either instantly or after. It helps to explain the function of a particular line or lines of code.

2.2.1    Single line commenting

Fortran

!Fortran program to display your name

 

2.3    Variable

Variable is used to store values in a program. When you create variable, it resides in the memory of the computer. Variable is a value that can change.

2.3.1    Rules for naming identifier

-         Give meaning names (your identifier must tally with what your program is all about)

-         Start with letter (a-z, or A-Z), number (0-9) or underscore

-         Identifier cannot start with a number (0-9)

-         Keywords or reserved words cannot be used to create identifier

2.4    Datatype

A data type specifies which type of value a variable has, what type of value can be stored in it considering the type of mathematical, relational or logical operations that can be applied to the variable without causing an error. Two categories of datatypes are primitive and non-primitive. We will focus on primitive.

Fortran

Character (len = 200) – stores combination of letters, numbers and special character. You can replace the 200 with lesser value depending on the information you want the user to enter.

character – stores single character like letter, number and special character

integer – stores whole number

real – stores decimal numbers

complex datatype

logical– stores true or false value (it serves as boolean type)

 

program lab

logical :: age

age = .True.

print *, age

end program lab

OUTPUT

T

TIP: you attach periods(.) to the beginning and end of the true or false keyword.

 

2.5    Variable declaration

Variable declaration is a statement used to specify the variable name and its data type.

 

Fortran

Syntax

datatype :: variableName

//single variable

character(len = 200) :: firstname

integer :: age

//multiple variables

character(len = 200) :: firstname, surname

integer :: age, level

 

2.6    Variable Initialization

Initializing a variable means to specify an initial value to assign to it. A variable that is not initialized is stored with a default value. integer is stores 0, real stores 0.0, character stores ‘\u0000’, logical stores false. Two types of variable initialization are explicit and implicit. Explicit initialization is when you assign a value in a program in the source editor before compiling the program, while implicit initialization is when you supply value after the program compiles.

Fortran

variableName = value

firstname = ‘dmactutor’

age = 18

 

2.7    Variable Declaration and Initialization

If you don’t want to first declare a variable then initialize it, you can also make a 2-in-1 statement, by declaring and initializing a variable at the same time.

Fortran

datatype :: variableName = value

character(len = 200) :: firstname = ‘dmactutor’

integer :: age = 18

 

2.8    Constant Variable Declaration and Initialization

If you don’t want a value to change, you can declare it as constant, or if you want to use a value repeatedly in program, for example pi = 3.14, the value is constant.

Fortran

Syntax

datatype, parameter :: variableName = value

real, parameter :: pi = 3.14

 

UNIT 3

CODE IMPLEMENTATION USING FORTRAN 95

3.1       PROGRAMS WITH LAB EXAMPLES

TIP: Don’t read alone, ensure you type and run this program so as to enhance your programming skills

3.1.1   Lab 1 – program to display name and age

Fortran

program lab1

print *, 'dmactutor'

print*, 18

end program lab1

 

3.1.2   Lab 2 – program to declare and initialize name and age

Fortran

program lab2

 !declare variable

character(len = 200) :: fullname

integer :: age

 

!initialize variable

fullname = 'dmactutor'

age = 18

 

!display

print *, fullname

print*, age

end program lab2

 

3.1.3   Lab 3 – program to declare and accept input for variables name and age

Fortran

program lab3

 

!declare variable

character(len = 200) :: fullname

integer :: age

 

!user action (what user should do) and accept input

print *, 'Enter your full name'

read*, fullname

print *, 'Enter your age'

read*, age

 

!display

print *, fullname

print*, age

 

!to make it descriptive

print *,'Your name is:: ', fullname

end program lab3

 

3.1.4   Lab 4 – program to add two numbers (arithmetic operation)

Fortran

program lab4

 

!declare variable

integer :: fnum, snum, result

 

!user action (what user should do) and accept input

print *, 'Enter first value'

read*, fnum

print *, 'Enter second value'

read*, snum

 

!processing

result = fnum + snum

 

!display

print *,'The addition is:: ', result

end program lab4

 

3.1.5   Lab 5 – program to compute mathematical formulas getting decimals. Program for bmi (body max index).

The appropriate datatype for fortran: realand formatting it to two decimal places. Test it with integer for Fortran to see what will happen.

Fortran

program lab5

 

!declare variable

real :: weight, height, resultBmi

 

!user action (what user should do) and accept input

print *, 'Enter weight value in KG'

read*, weight

print *, 'Enter height value in meters from (0-2)'

read*, height

 

!processing

resultBmi = weight / (height * height)

 

!before formatting

print *,'The BMI is:: ', resultBmi

 

!after formatting

print *,'The BMI is:: '

print '(f6.2)', resultBmi

end program lab5

 

3.1.6   Lab 6 – program to compute mathematical formulas using inbuilt function. Program for Pythagoras theorem to find the hypothenus.

We will use sqrt() built-in function. The sqrt() function returns the square root of its argument. Fortran allows ** as powr operator. For example, opp square (opp^2) in fortran you can use opp**2

Fortran

program lab6

 

!declare variable

real :: opp, adj, hyp

 

!user action (what user should do) and accept input

print *, 'Enter opposite value'

read*, opp

print *, 'Enter adjacent value'

read*, adj

 

!processing

hyp = sqrt((opp*opp) + (adj*adj));

 

!before formatting

print *,'The hypothenuse result is:: ', hyp

 

!after formatting

print *,'The hypothenuse result is:: '

print '(f6.2)', hyp

end program lab6

 

Comments

Popular posts from this blog