









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
simple introduction about Fortran
Typology: Exercises
1 / 17
This page cannot be seen from the preview
Don't miss anything!










Introduction to Fortran language. Constants and variables. Declaration of variables. Library functions. Mixing of variable types. Control statements:
Fortran is the abbreviation of “formula translation” and it’s a compiled programming language, that is mainly used for numeric computation and scientific computing.
Workspace: it’s an environment in which files are stored and it’s considered to be a hub for all the programs created within it. Text file: it’s the space in which the program (code) is written. Variable: every data stored within the computer memory can be labeled by giving it a name, and this name is the variable. Variables can be real numbers, integers, or texts.
Integer. Real. Character. Complex. Logical.
Example: Write a computer program to multiply two values.
Program example implicit none
real :: a , b , c
read *, a read *, b
c = a * b
print *, c
End program example
Hint: When declaring a variable, don’t declare it as a letter e.g. (x, r, a, etc.). Instead, declare it as a meaningful word e.g. (length, area, radius, etc.)
Hint : Use navigation messages in order to let the user know the data that must be entered into the program.
Classwork: Write a computer program to calculate the radius of a circle by entering a value for the radius.
Program Circle_Area implicit none real :: area , radius print *, “Enter the value of the radius” read *, radius area = 3.14159 * radius ** 2 print *, area end program Circle_Area
Example: write a computer program to switch between to values.
Solution:
Program Switch implicit none real :: a , b , c read *, a , b c = a a = b b = c print *, a , b end
Navigation message
1- Parentheses. ( ) 2- Library functions. 3- Power. (*) 4- Multiplication and division. ( & /) 5- Addition and subtraction. (+ & -)
Example: write a computer program to calculate the value of Y given by the following equation:
Solution:
Program calculate_y implicit none real :: y y = (sin(3.14159/6))3-sqrt(150(1.0/3)/(33.14159*2)) print *, y end program calculate_y
Note : in order to declare a parameter, we will write this piece of code:
Parameter type, parameter :: parameter’s name = parameter’s value
e.g. integer, parameter :: x = 5
Classwork: write a computer program to calculate the value of y given by the following equation:
√𝑦 3
Solution:
Program calculate_y implicit none real :: y , x read , x y = (3abs(((sin(x))2-(3x))/(x+(cos(x/3.14159))2)))* print *, y end program calculate_y
Greater than or equals to
Less than or equals to .and. And .or. Or .not. not
There are four types of conditional statements
1- IF (condition) statement
In this type of conditional statements, only one statement is written and it would be executed if the condition was true
Example: Write a computer program that tells if a student has passed a test by giving the student’s mark.
Solution:
Program status implicit none real :: mark read *, mark if (mark >= 50) print *, “The student has passed the test” end program status
3- IF (condition) then statement # else statement # end if
In this type the first statement will be executed, if the condition is true. Otherwise, the second statement will be executed.
Example: Write a computer program that tells if a student has passed or failed a test by giving the student’s mark.
Program status implicit none real :: mark read *, mark if (mark >= 50) then print *, "The student has passed the test" else print *, “The student has failed the test” end if end program status
4- IF (condition) then statement # else if (condition) then statement # else if (condition) then statement # end if
Example: Write a computer program that tells if a student has passed or failed a test by giving the student’s mark.
Program status implicit none real :: mark read *, mark if (mark >= 50) then print *, "The student has passed the test" else if (mark < 50 ) then print *, “The student has failed the test” end if end program status
Classwork: Write a computer program that estimates a student’s mark depending on their status (either “passed” or “failed”):
Solution:
program evaluator implicit none character * 50 :: status read *, status if (status == "passed") then print *, "The mark is equal to or above 50" else print *, "The mark is below 50" end if end program evaluator
Homework: Write a computer program to evaluate a student’s mark.
Solution:
Program evaluation implicit none real :: mark if (mark > 89) then print *, “The evaluation is Excellent” else if ( mark <= 89) .and. (mark >= 80) then print *, “The evaluation is very good” else if ( mark <= 79) .and. (mark >= 70) then print *, “The evaluation is Good” else if ( mark <= 69) .and. (mark >=60) then
print *, “The evaluation is intermediate” else if ( mark <= 59) .and. (mark >= 50) then print *, “The evaluation is Acceptable” else print *, “The evaluation is Failed” end if end