Fortran Practice Exercises: Essential Techniques for Mastering the Framework

Here’s a simple practice exercise for Fortran that will help you get more familiar with the language:

Write a program that calculates the area of a circle. The program should prompt the user to enter the radius of the circle, then it should calculate and print the area.

Here’s an example of what the output should look like:

yamlCopy codeEnter the radius of the circle: 5.0
The area of the circle is: 78.539816339744831

Here’s some starter code to help you get started:

sqlCopy codePROGRAM CircleArea
  REAL :: radius, area
  REAL, PARAMETER :: pi = 3.14159
  WRITE(*,*) 'Enter the radius of the circle:'
  READ(*,*) radius
  ! Calculate the area of the circle
  area = pi * radius**2
  WRITE(*,*) 'The area of the circle is:', area

Save the code in a .f90 or .f95 file, then compile and run it using your Fortran compiler. This simple exercise will help you practice using variables, mathematical expressions, input and output statements, and basic program flow control in Fortran.

Tags: No tags

Add a Comment

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