Fortran Example: Unveiling the Power of Code with Simple Examples
Fortran is one of the oldest and most powerful programming languages, widely used for scientific and engineering applications. Despite newer languages emerging, Fortran continues to hold a strong position in areas where performance and efficiency matter most. In this article, we will explore several Fortran examples, demonstrating the language's versatility and why it remains a top choice for computational tasks.
Why Learn Fortran? A Quick Overview
Fortran, short for "Formula Translation," was first developed in the 1950s by IBM to perform complex mathematical and scientific computations. Its power lies in its ability to handle large amounts of data and complex calculations with exceptional speed and efficiency. Over the years, Fortran has evolved into a modern programming language, capable of handling everything from basic tasks to cutting-edge supercomputing simulations.
In the world of high-performance computing (HPC), Fortran continues to be the language of choice, especially in disciplines like physics, engineering, climate modeling, and finance. Its ongoing evolution, including versions like Fortran 90, 95, and 2003, has introduced features such as object-oriented programming and improved support for modern hardware. However, despite these advancements, Fortran maintains its focus on numerical and scientific computing.
Basic Fortran Example: Hello World
Let’s start with a simple Fortran program that prints "Hello, World!" to the console. This example will give you a quick feel for Fortran's basic syntax and structure.
program hello_world print *, "Hello, World!" end program hello_world
In this example, the program uses the `print` statement to display a message on the screen. It's a basic starting point for anyone learning Fortran, and it's simple to understand, even for beginners.
Example 2: Calculating the Factorial of a Number
Now, let’s dive into a slightly more complex example that calculates the factorial of a number using a recursive function. Factorial calculations are common in mathematics, and implementing them in Fortran helps demonstrate how to work with functions and recursion.
program factorial
implicit none
integer :: n, result
! Ask the user for an input
print *, "Enter a number to calculate its factorial:"
read(*,*) n
! Calculate the factorial
result = factorial_function(n)
! Print the result
print *, "The factorial of ", n, " is ", result
contains
! Recursive function to calculate factorial
function factorial_function(n)
integer :: n
integer :: factorial_function
if (n == 0) then
factorial_function = 1
else
factorial_function = n * factorial_function(n - 1)
end if
end function factorial_function
end program factorial
In this program, we ask the user for an input and then calculate the factorial of the number using recursion. This example showcases how Fortran supports functions and how recursive calls can be made.
Example 3: Working with Arrays in Fortran
One of the key features of Fortran is its powerful array handling. Let’s take a look at an example where we define a 2D array and calculate the sum of its elements. This is a typical task in scientific computing, where large data structures need to be manipulated efficiently.
program array_sum
implicit none
integer, dimension(3, 3) :: matrix
integer :: i, j, total
! Initialize the matrix
matrix = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], shape(matrix))
! Calculate the sum of the elements
total = 0
do i = 1, 3
do j = 1, 3
total = total + matrix(i, j)
end do
end do
! Print the result
print *, "The sum of all elements in the matrix is ", total
end program array_sum
This program demonstrates how to create a 3x3 matrix (2D array) in Fortran and calculate the sum of all its elements. Fortran’s syntax makes working with arrays simple and efficient, making it a great choice for numerical computing.
Example 4: Solving a System of Linear Equations
Fortran excels in solving complex mathematical problems, including linear algebra. Below is an example of solving a system of linear equations using matrix operations. This example is typical of the kinds of tasks Fortran is used for in scientific computing.
program solve_system
implicit none
real, dimension(3, 3) :: A, L, U
real, dimension(3) :: b, x
integer :: i, j
! Define matrix A and vector b
A = reshape([4, -2, 1, -2, 4, 0, 1, 0, 3], shape(A))
b = [1, 4, 3]
! Perform LU decomposition
call lu_decomposition(A, L, U)
! Solve the system L * U * x = b
call solve_lu(L, U, b, x)
! Print the result
print *, "The solution is: ", x
contains
subroutine lu_decomposition(A, L, U)
real, dimension(3, 3) :: A, L, U
integer :: i, j, k
real :: sum
! Initialize L and U
L = 0.0
U = 0.0
! LU decomposition
do i = 1, 3
U(i, i) = A(i, i)
L(i, i) = 1.0
do j = i+1, 3
L(j, i) = A(j, i) / U(i, i)
U(i, j) = A(i, j)
end do
end do
end subroutine lu_decomposition
subroutine solve_lu(L, U, b, x)
real, dimension(3, 3) :: L, U
real, dimension(3) :: b, x
real :: sum
integer :: i, j
! Solve L * y = b
call forward_substitution(L, b, x)
! Solve U * x = y
call backward_substitution(U, x, x)
end subroutine solve_lu
subroutine forward_substitution(L, b, x)
real, dimension(3, 3) :: L
real, dimension(3) :: b, x
real :: sum
integer :: i, j
do i = 1, 3
sum = b(i)
do j = 1, i-1
sum = sum - L(i, j) * x(j)
end do
x(i) = sum / L(i, i)
end do
end subroutine forward_substitution
subroutine backward_substitution(U, y, x)
real, dimension(3, 3) :: U
real, dimension(3) :: y, x
real :: sum
integer :: i, j
do i = 3, 1, -1
sum = y(i)
do j = i+1, 3
sum = sum - U(i, j) * x(j)
end do
x(i) = sum / U(i, i)
end do
end subroutine backward_substitution
end program solve_system
This program demonstrates solving a system of linear equations using LU decomposition—a common method used in scientific computing. The ability to handle such mathematical operations makes Fortran a valuable tool in the world of research and simulation.
Conclusion: The Continuing Relevance of Fortran
Fortran programming continues to be relevant and powerful today, especially for those working in scientific, engineering, and high-performance computing domains. Its ability to handle complex numerical calculations, manage large datasets, and integrate seamlessly with other languages ensures that Fortran remains an essential tool in many industries.
With its rich history, ongoing development, and vast community of developers, Fortran will likely continue to thrive for many years. Whether you’re a researcher, scientist, or engineer, learning Fortran can open doors to a world of computational power, enabling you to solve some of the most challenging problems in science and engineering.
So, if you haven’t already, now is a great time to dive into the world of Fortran programming and start exploring its potential with these examples!

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