FORTRAN Tutorial Lesson 5 | Problem Solving 1

Welcome to the first post for problem solving in FORTRAN.

I strongly recommend watching my previous lessons.

Check my FORTRAN programming page for more tutorials.

If you are confident enough about the previous lessons, lets dive right to the problems.

Table Of Contents

Question 1:

Find the mid-point of (2,4) and (4, -10).

To find the mid-point, we have formula: (x, y) = (\frac{x_1 + y_1}{2}, \frac{x_2+y_2}{2})

Lets see the steps written in comments first:

program mid_point
 implicit none
  ! variable x1, x2, y1, y2 declaration. They are the input 

  ! variable x, y declaration. let that be output

  ! writing formula for x 

  ! writing formula for y
  ! printing out the value of x and y

end program

First try yourself to write a code taking hint from above.

Look at the code below for any correction to your code.

program mid_point
  implicit none
   real :: x1, x2, y1, y2
   real :: x, y
     x1 = 2; y1 = 4; x2 = 4; y2 = -10
     x = (x1 + y1) / 2.0
     y = (x2 + y2) / 2.0
    print*, "The mid point is ", x, y
end program

The output to the above code is:

The mid point is:  3.00000000  -3.00000000

If your answer is right, then your problem solving skill is good. Lets sharp them by another question.

Question 2

Find the Gravitational Force exerted by the earth on 1 kg mass lying on the earth's surface.
earth mass = 6 \times 10^{24}
earth radius = 6400 km
universal gravitational constant = 6.67 \times 10^{-11}

We have formula: F = \frac{GMm}{R^2}

Our Formula has two quantity ‘m’ and ‘M’, which are recognized as the same in FORTRAN because FORTRAN is case insensitive. So lets change the formula to :

F = \frac{Gm_1m_2}{R^2}

First try to write yourself.

Check the below code for correction to your code.

program force
  implicit none
    real, parameter :: G = 6.67E-11    ! for power of ten, write e OR E.....e-11 = 10^{-11}
    real :: m1, m2, R     ! given value: m1 = mass of earth, m2 = mass of object, R = radius of earth
    real :: F      ! output variable F = force
    ! assigning values
     m1 = 6*10**24
     m2 = 1
     R = 6400*1000
      F = (G*m1*m2) / R**2
      print*, "The force is", F
end program

The output is:

The force is   683.935547

Now you are confident in problem solving using FORTRAN.

Examples of complex data type

Complex number has two parts: real and imaginary part. In FORTRAN, we write complex number as (real, imaginary).

We can perform basic math operation in complex data type like in integer and real.

Below is code that shows the uses of complex numbers.

program complex_math
   implicit none
   complex :: a, b     ! declaring variable a and b as complex data type
     a = (3, 4)      ! (3, 4) is 3 + 4i
     b = (1, -2)      ! (1, -2) is 1 - 2i
     print*, a+b, a-b, a*b, a/b
end program


(4.00000000, 2.00000000)    (2.00000000, 6.00000000)     (11.00000000, -2.00000000)    (-1.00000000, 2.00000000)

FORTRAN performs such complications complex multiplication and vision easily, just like ordinary math.

Examples of character data type

Character data means the text or string.

Each letter or number or special character is a character. Combination of character becomes text.

While declaring character data type, we have to provide another specifier len. Then len = "value"controls the length of the text. for eg.

program character_example
  implicit none
   character(len=4) :: text
   text = "welcome"
   print*, text
end program


welc              ! because of len=4
len = 1 gives output w
len = 2 gives output we
len = 3 gives output wel
len = 4 gives output welc
len = 5 gives output welco
len = 6 gives output welcom
len = 7 gives output welcome

So while using len= "value", value should be greater than the length of text.

Example of logical data type

Logical data type in FORTRAN has only two values. They are: .true. and .false.

program logicalUse
  implicit none
   logical :: correct, wrong
    correct = .true.
    wrong = .false.
 print*, correct
print*, wrong
end program

The output is:


Logical data type are not used for any calculations, rather it used to check conditional statements.

Not Clear Enough? Watch the Video on problem solving 1.

Share this post:

Leave a Comment

Your email address will not be published. Required fields are marked *