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:
Enter 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:
PROGRAM 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 END PROGRAM CircleArea
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.
Add a Comment