program myfirstprogram

* * = comment. Comments are GOOD!
* character type : words,letters,etc.
* real      type : any number with decimal points 
* integer   type : any integer number. DON'T USE REAL AND INTEGER inter-  
* changably
* logical   type : True or False
* complex   type : any complex number

* parameter             : a fixed constant within the code
* character*m           : a string with length m
* variablename (n)      : variable is defined to be an array of size (n)
*                       : The array complex_roots is an array with 2 "spaces"

      character*30 myword
      real area,r,a,b,c,disc,pi
      integer d,factorial
      complex complex_roots(1:2)
      parameter (pi=3.14)

* format statement:
* used to format numbers, strings in I/O (Input/Output) on screen and to files
* format(a30)   : read a string of 30 characters long
* format(i1)    : a single integer digit
* format(f15.2) : a real number with a total of 15 digits (including decimal point); 
*               : ".2" says there are total 2 decimal numbers (including the decimal point)
*               : (i.e. .5 is a "f2.2") 

* basic I/O
* read           : read data from a file/user input
* write          : write data to a file/screen
* open (unit=10,file="myfile.dat") : open the file "myfile.dat"
* options with "open"              : form=data type (binary?);status="old","new"; a couple of more
* close (unit=10)                  : close unit 10 (in this case "myfile.dat") 
* read/write (*)                   : in/from screen
* read/write (unit=10)             : in/from "unit=10"; "unit" previously defined (usually a file) 

*
* getting a text input from user
*

      write (*,*) 'What is your name?'
      read (*,*) myword 

      write (*,*) 'How are you doing, ',myword,'?'

*
* this is a do loop
*

      factorial=1
      d = 1
      
      do 50 d=1,5

         factorial=factorial*d
         write (*,*) d,'! = ',factorial 
 50      continue


*
* demonstration of a very simple user defined function
*

      write (*,*) 'Input a radius :'
      read  (*,*) r

*
* circlearea = a user defined function
*

      area=circlearea(r)

      write (*,*) 'The area of the circle is:'
      write (*,100) area

 100  format (f12.2)

      
*
* a demo on the usage of complex numbers and internal math functions of fortran
*
      write (*,*) 'Any quadratic equation of the form ax^2+bx+c=0'
      write (*,*) 'has two solutions:'
      write (*,*) 'a) (b + (b^2-4*a*c)^0.5) / (2*a)'
      write (*,*) 'b) (b - (b^2-4*a*c)^0.5) / (2*a)'
      write (*,*) 'Complex solutions arises if b**2-4ac < 0'

      write (*,*) 'Enter a:'
      read (*,*) a

      write (*,*) 'Enter b:'
      read (*,*) b

      write (*,*) 'Enter c:'
      read (*,*) c

      disc=b*b-4*a*c

      if (disc.ge.0.00) then
         write (*,*) 'This quadratic equation has real roots.'
      else
         write (*,*) 'This quadratic equation has complex roots.'
      end if

* function complex: complex (a,b) = a + i * b, i = sqrt (-1) 
* function sqrt   : sqrt (a) = positive square root of a

      complex_roots(1)=(-complex(b,0)+sqrt(complex(disc,0)))/(2.0*a)
      complex_roots(2)=(-complex(b,0)-sqrt(complex(disc,0)))/(2.0*a)

      write (*,*) 'The roots are : '
      write (*,*) complex_roots(1)
      write (*,*) complex_roots(2)

      call writefile(complex_roots,a,b,c)

      stop
      end

*
*     subroutine
*


      subroutine writefile(complexvalue,n1,n2,n3)
      
      complex complexvalue(1:2)
      real n1,n2,n3

*
* How to write text to a file
*

      open (unit=10,file='data.dat')

      write (10,107) 'a = ',n1
      write (10,107) 'b = ',n2
      write (10,107) 'c = ',n3
      
      write (10,108) 'c1= ',complexvalue(1)
      write (10,108) 'c2= ',complexvalue(2)

* Format statements for complex numbers have 2 components

 107  format(a4,f8.2)
 108  format(a4,2(f8.2))

      close (unit=10)

      return
      end

*     a function

      real function circlearea(radius)

      real radius,area,pi
      parameter (pi=3.141)
      
      area = (radius**2)*pi
      circlearea = area

      return
      end