Fortran – Create Array and Sort Given Numbers

email me

I used the registered version of the Simply Fortran compiler, here: simplyfortran-3.0.msi,  simplyfortran-2.35.msi; also, try the online Fortran compiler.

dimension a(100)

! declare strings
character(LEN=18) :: strLine1
character(LEN=24) :: strLine2

! set strings
strLine1 = "Enter array size: "
strLine2 = "Enter numbers in array: "

! display string 1
PRINT *, strLine1

! input 1
write(*,*)
read(*,*)n

! display string 2
PRINT *, strLine2

! input 2
write(*,*)
read(*,*)(a(i),i=1,n)

! sort
do i=1,n-1
    do j=i+1,n
        if (a(i).gt.a(j)) then
            t=a(i)
            a(i)=a(j)
            a(j)=t
        end if
    end do 
end do

write(*,*)
write(*,*) (a(i),i=1,n)

read(*,*)
stop
end

 

Output

 

Notes

Learn more, Fortran language

https://pinetools.com/syntax-highlighter

 

Types of Variables

Integer: It can hold only integer values.

Real: It stores the floating point numbers.

Complex: It is used for storing complex numbers.

Logical: It stores logical Boolean values.

Character: It stores characters or strings.