Fortran 95 Source Code

! Author: Hannes du Plooy 
! Date: 20 Sep 2016       
! Objective: To solve the towers of hanoi with number of discs provided in the arguments to calling the program 
program HanoiProg
    character(len=2)::arg
    integer::discs
    
    if (command_argument_count() < 1) then
        print *,"Please provide number of discs!"
    else
        call get_command_argument(1,arg)
        read (arg,'(i2)'), discs
        call hanoi(0,2,1,discs)
    end if
    
contains
    recursive subroutine hanoi(source,dest,other,discs)
        integer :: source
        integer :: dest
        integer :: other
        integer :: discs
        
        if (discs > 0) then
            call hanoi(source,other,dest,discs-1)
            print '(a,$)','Move '
            print '(i2,$)',source
            print '(a,$)',' to '
            print '(i2)',dest            
            call hanoi(other,dest,source,discs-1)
        end if
    end subroutine

end program HanoiProg