Fortran Code Example: Discover Simple and Powerful Techniques
Fortran is one of the oldest and most powerful programming languages, still used widely in scientific computing, numerical methods, and high-performance applications. Whether you are a beginner or an experienced developer, learning Fortran can be a rewarding experience, and understanding the language's core syntax and structure is essential for any aspiring programmer. In this article, we'll explore several Fortran code examples, discussing key concepts and techniques that can help you write efficient, effective programs. Ready to jump in? Let’s go!
What Is Fortran?
Fortran (short for Formula Translation) was developed in the 1950s and has since become one of the most important programming languages for numerical computation. It is particularly popular in areas such as engineering, physics, weather modeling, and computational biology. Despite its age, Fortran has evolved and continues to be a vital part of modern computing, thanks to its high performance and strong support for scientific libraries and parallel computing.
One of the reasons Fortran has stood the test of time is its focus on mathematical operations and optimization. With the ability to handle large datasets and execute complex mathematical calculations, it remains a top choice for high-performance computing (HPC) applications. In this article, we will focus on practical examples that highlight the power of Fortran, from basic syntax to more advanced techniques.
Basic Fortran Syntax
Before diving into code examples, let’s briefly review some basic elements of Fortran syntax. Understanding these will help you follow the examples and write your own programs with ease:
- Variables and Types: Fortran has a range of data types, such as INTEGER, REAL, DOUBLE PRECISION, CHARACTER, and LOGICAL. It’s important to choose the correct data type for your variables to ensure efficient memory usage and computation.
- Comments: Comments are written with an exclamation mark (
!) and are ignored by the compiler. For example:! This is a comment. - Program Structure: A typical Fortran program begins with the
programkeyword and ends withend program. Code within the program can be divided into functions and subroutines.
Now that you are familiar with some of the basics, let’s dive into some practical code examples to demonstrate the power and flexibility of Fortran.
Fortran Code Example 1: Hello World
Let’s start with a simple “Hello, World!” program. This is often the first program written in any language, as it demonstrates the basic structure and syntax of a language.
program hello_world
print *, 'Hello, World!'
end program hello_world
In this example, the print * statement outputs the text “Hello, World!” to the console. The program keyword defines the start of the program, while end program signifies the end. This is the simplest form of a Fortran program, but it’s a great way to start learning the syntax!
Fortran Code Example 2: Calculating the Factorial
Next, let’s look at a more practical example: calculating the factorial of a number. The factorial of a number is the product of all positive integers less than or equal to that number. For example, the factorial of 5 (denoted as 5!) is 5 * 4 * 3 * 2 * 1 = 120.
program factorial
integer :: n, result
print *, 'Enter a number:'
read *, n
result = 1
do i = 1, n
result = result * i
end do
print *, 'The factorial of ', n, ' is ', result
end program factorial
This program first asks the user to input a number and then calculates its factorial using a do loop. The loop runs from 1 to n, multiplying the current value of result by i each time.
Fortran Code Example 3: Working with Arrays
Fortran has excellent support for arrays, which makes it ideal for working with large datasets. In this next example, we will create a program that calculates the sum of all elements in an array.
program sum_array
integer :: i, sum
integer, dimension(5) :: arr = [1, 2, 3, 4, 5]
sum = 0
do i = 1, 5
sum = sum + arr(i)
end do
print *, 'The sum of the array is ', sum
end program sum_array
This program creates an integer array arr with 5 elements. The do loop iterates over each element of the array, adding its value to the sum variable. After the loop finishes, the sum of the array is displayed.
Fortran Code Example 4: Working with Functions
Fortran allows you to create functions that return values, making it easy to modularize your code. In this example, we will define a function that calculates the area of a circle given its radius.
program circle_area
real :: radius, area
! Call the function to calculate the area
print *, 'Enter the radius of the circle:'
read *, radius
area = calculate_area(radius)
print *, 'The area of the circle is ', area
contains
! Function to calculate the area
real function calculate_area(r)
real :: r
calculate_area = 3.14159 * r * r
end function calculate_area
end program circle_area
In this program, we define a function calculate_area within the contains section. This function takes the radius as an argument and returns the area of the circle. The function is then called from the main program, and the result is displayed.
Fortran Code Example 5: Using Do Loops for Summation
In this example, we will use a do loop to calculate the sum of all even numbers from 1 to a user-defined limit. Loops like this are essential for performing repetitive calculations or processing large amounts of data.
program sum_even_numbers
integer :: i, limit, sum
print *, 'Enter the limit:'
read *, limit
sum = 0
do i = 2, limit, 2
sum = sum + i
end do
print *, 'The sum of even numbers up to ', limit, ' is ', sum
end program sum_even_numbers
Here, the do loop runs from 2 to the user-defined limit, with a step of 2. This ensures that only even numbers are added to the sum variable. The final sum is printed at the end.
Fortran Code Example 6: File Handling
File handling in Fortran allows you to read from and write to files, which is essential for processing large datasets or storing results. In this example, we’ll read numbers from a file, calculate their sum, and output the result to another file.
program file_sum
integer :: num, sum, unit
open(unit=10, file='input.txt', status='old')
open(unit=20, file='output.txt', status='unknown')
sum = 0
do while (.true.)
read(10, *, iostat=ierr) num
if (ierr /= 0) exit
sum = sum + num
end do
write(20, *) 'The sum of numbers is ', sum
close(10)
close(20)
end program file_sum
This program opens an input file called "input.txt" and reads integers from it. It keeps a running total of the sum of the numbers and writes the result to an output file called "output.txt". The iostat variable is used to detect the end of the file.
Conclusion
Fortran remains a powerful tool for numerical computation and scientific programming, offering a variety of features and techniques that are still relevant today. In this article, we’ve covered several Fortran code examples that demonstrate the language’s simplicity and versatility, from basic syntax to more complex tasks like file handling and function creation.
Whether you're a beginner just starting to learn Fortran or an experienced developer looking to improve your skills, mastering these concepts will set you on the path to writing effective and efficient Fortran programs. Keep experimenting with different examples, and you'll soon unlock the full potential of this amazing language!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!