TechnoByte, The Students Forum
   Fortran Programming

What is fortran?

FORTRAN, stands for Formula Translation, computer programming language created in 1957 by John Backus that shortened the process of programming and made computer programming more accessible. Fortran formerly FORTRAN is a general-purpose, compiled imperative programming language that is especially suited to numeric computation and scientific computing.

Fortran was originally developed by IBM in the 1950s for scientific and engineering applications, and subsequently came to dominate scientific computing. It is a popular language for high-performance computing and is used for programs that benchmark and rank the world's fastest supercomputers. The main enhancement was to support procedural programming by allowing user-written subroutines and functions which returned values with parameters passed by reference.

Early FORTRAN compilers supported no recursion in subroutines. Early computer architectures supported no concept of a stack, and when they did directly support subroutine calls, the return location was often stored in one fixed location adjacent to the subroutine code (e.g. the IBM 1130) or a specific machine register (IBM 360 et seq), which only allows recursion if a stack is maintained by software and the return address is stored on the stack before the call is made and restored after the call returns.

Array Declaration in fortran

Arrays are declared with the dimension attribute.
For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, we write,
real, dimension(5) :: numbers
The individual elements of arrays are referenced by specifying their subscripts. The first element of an array has a subscript of one. The array numbers contains five real variables: numbers(1), numbers(2), numbers(3), numbers(4), and numbers(5).
To create a 5 x 5 two-dimensional array of integers named matrix, we write −
integer, dimension (5,5) :: matrix


Functions and Subroutines in fortran

Functions and subroutines operate similarly but have one key difference. A function is used when a value is returned to the calling routine, while a subroutine is used when a desired task is needed, but no value is returned.
A Fortran function is similar to a mathematical function, which takes one or many parameters as inputs and returns a single output value. A Fortran subroutine is a block of code that performs some operation on the input variables, and as a result of calling the subroutine, the input variables are modified.


Q.1 > Write a fortran program find to sum of two number.

program addnum
   implicit none
   real :: a, b, result
   a = 12.5
   b = 15.8
   result = a + b
   print *,"The total is: ", result
end program addnum


Q.2 > Write a fortran program to print the size of real and integer variable in fortran.

program testn
   implicit none
   real :: num1
   integer :: num2
   print *,huge(num1)
   print *,huge(num2)
end program testn


Q.3 > Write a fortran program to store a string in a character array and print it back.

program testchar
   implicit none
   character (len = 40) :: str
   str = "Programming in fortran is fun"
   print *,"Hello ", str
end program testchar


Q.4 > Write a fortran program to find the gratest among two numbers.

program findgr
   implicit none
   real :: a, b
   a = 12.5
   b = 15.8
   if (a .gt. b) then
      print *,"The greatest is: ", a
   else
      print *,"The greatest is: ", b
   end if
end program findgr


Q.5 > Write a fortran program to print first 10 natural numbers.

program num10
   implicit none
   integer :: num
   num = 1
   do while (num .le. 10)
      print *, num
      num = num + 1
   end do
end program num10


Q.6 > Given a trangle with sides a, b, c. Write a fortran program to find out whether it is an isoscals trangle or not.


Q.7 > Write a fortran program to cheak a leap year or not.


Q.8 > Write a fortran program to create an integer array acept number from the user and print them back.

program array
   implicit none
   integer, dimension (5) :: num
   integer :: i,n
   do i=1,5
      print *,"Enter a number: "
      read *,num(i)
   end do
   do i=1,5
      print *,num(i)
   end do
end program array


Q.9 > Write a fortran program to create a double dimentional array of 5 row and columns store number for 1 to 25 and print them back.

program ddarray
   implicit none
   integer, dimension (5, 5) :: num
   integer :: i,j
   do i=1,5
      do j=1,5
         print *,"Enter a number: "
         read *,num(i, j)
      end do
   end do

   do i=1,5
      do j=1,5
         print *,num(i,j)
      end do
   end do
end program ddarray


Q.10 > Write a fortran program subroutine to multiply 2 square matrix.


Q.11 > Write a fortran program sorting of a given set of numbers in assending order.

program arraysort
   implicit none
   integer, dimension (10) :: num
   integer :: i, j, tmp
   do i=1,10
      print *,"Enter a number: "
      read *,num(i)
   end do

   do i=1, 9
      do j=1, 10 - i
         if(num(j) .gt. num(j+1)) then
            tmp=num(j)
            num(j)=num(j+1)
            num(j+1)=tmp
         end if
      end do
   end do

   do i=1,10
      print *,num(i)
   end do
end program arraysort


Q.12 > Write a fortran program for the following matrices addition, multiplication, substraction.


Q.13 > Write a fortran program to read 2 integers i and j and print the quotient and remainder when i is divided by j.


Q.14 > Write a fortran program to find the number of non-blank character in a string of length 20.


Q.15 > Write a fortran program to find the revers of a string.

program revstr
   implicit none
   character (len = 40) :: str
   character (len = 40) :: str2
   integer i, j, x
   print *,"Enter a string: "
   read *,str
   x=len(trim(str))
   j=x

   do i=1,x
      str2(i:i)=str(j:j)
      j=j-1
   end do
   print *,"The reverse string is ", str2
end program revstr


Q.16 > Write a fortran program to count the number of words in a sentence.

program countword
    implicit none
    character (len = 40) :: str
    integer i, x, wordcount
    wordcount = 1
    print *,"Enter a string: "
    read (*,'(A)') str
    x=len(trim(str))
    do i=1,x
        if(str(i:i) == ' ') then
        wordcount = wordcount + 1
        end if
    end do
    print *,"Total number of words are ", wordcount
end program countword


Q.17 > Write a fortran program for decimal equivalent of remain numbers.


Q.18 > Write a fortran program using function which calculates and returns the distance between any 2 points whose coordinates are (x1,y1) and (x2,y2).


Q.19 > Write a fortran program to write the algorithem to find largest of 3 numbers.

program greatestnum
    implicit none
    integer :: a, b, c
    print *,"Enter first number: "
    read *,a
    print *,"Enter second number: "
    read *,b
    print *,"Enter third number: "
    read *,c
    if (a .gt. b .and. a .gt. c) then
        print *, "The greatest is ", a
    else if (b .gt. c) then
        print *, "The greatest is ", b
    else
        print *, "The greatest is ", c
    end if
end program greatestnum


Q.20 > Write a fortran program to read a string of length up to 20 characters and determine whether it is a pallindrome.

program revstr
    implicit none
    character (len = 20) :: str
    integer i, j, x, flag
    flag=1
    print *,"Enter a string: "
    read *,str
    x=len(trim(str))
    j=x

    do i=1,x
        if(str(i:i) .ne. str(j:j)) then
             flag=0
             exit
        end if
        j=j-1
    end do
    if(flag == 1) then
        print *,"The string is palindrome"
    else
        print *,"The string is not a palindrome"
    end if
end program revstr


Q.21 > Write a fortran program to converts fahrenheit temprature to centigrade.


Q.22 > Write a fortran program to find aut factorial of a number using function.


Q.23 > Write a fortran program to find out the sum of digits of a input numbers.


Q.24 > Write a fortran program to read a sentence and reverse it.


Q.25 > Write a fortran program to read a sentence and convert all its character into their uper call letters.


Q.26 > Write a fortran program which prints all odd positive integers less then 100 omitting these integers divisible by 7.


Q.27 > Write a fortran program to calculate two strings into a single string.


Q.28 > Write a fortran program to reverse of a string.


Q.29 > Write a fortran program to find out the area of a trangle.


Q.30 > Write a fortran program to reverse of a string.


Q.31 > Write a fortran program to find out the area of a triangle.


Q.32 > Write a fortran program to compare 3 numbers.


Q.33 > Write a fortran program to compare function and subroutine.

program functionsubr
    implicit none
    real avragefunc
    integer :: a, b
    real :: res
    print *,"Enter first number: "
    read *,a
    print *,"Enter second number: "
    read *,b
    res = avragefunc(a, b)
    print *,"The average by func return is: ", res
    call subavrage(a, b)
end program functionsubr

real function avragefunc(x, y)
    implicit none
    integer :: x, y
    real :: avr
    avr = ( x + y ) / 3.0
    avragefunc = avr
    return
end

subroutine subavrage(x, y)
    implicit none
    integer :: x, y
    real :: avr
    avr = ( x + y ) / 3.0
    print *,"The average by subroutine call is: ", avr
    return
end


Q.34 > Write a fortran program to find the length of a string.


Q.35 > Write a fortran program to find avarage of any 5 numbers using array.

program arrayavg
    implicit none
    integer, dimension (5) :: num
    integer :: i
    real :: total, average
    do i=1,5
        print *,"Enter a number: "
        read *,num(i)
    end do
    total=0
    do i=1,5
        total = total + num(i)
    end do
    average = total / 5
    print *, "Average of the numbers is ", average
end program arrayavg


Q.36 > Write a fortran program to count the number of vowels in a sentence.

program countvowels
    implicit none
    character (len = 40) :: str
    integer i, x, vowelcount
    vowelcount = 0
    print *,"Enter a string: "
    read (*,'(A)') str
    x=len(trim(str))
    do i=1,x
        if(str(i:i) == 'a' .or. str(i:i) == 'e' .or. str(i:i) == 'i' .or. str(i:i) == 'o' .or. str(i:i) == 'u') then
        vowelcount = vowelcount + 1
        end if
    end do
    print *,"Total number of vowels are ", vowelcount
end program countvowels