Friday, November 26, 2010

FoRtRaN: Intrinsic function of fortran

FoRtRaN: Intrinsic function of fortran: "Function Meaning Arg. Type Return Type ABS(x) absolute value of x ..."

Intrinsic function of fortran

  • Function Meaning Arg. Type Return Type
    ABS(x) absolute value of x INTEGER INTEGER
    REAL REAL
    SQRT(x) square root of x REAL REAL
    SIN(x) sine of x radian REAL REAL
    COS(x) cosine of x radian REAL REAL
    TAN(x) tangent of x radian REAL REAL
    ASIN(x) arc sine of x REAL REAL
    ACOS(x) arc cosine of x REAL REAL
    ATAN(x) arc tangent of x REAL REAL
    EXP(x) exp(x) REAL REAL
    LOG(x) natural logarithm of x REAL REAL
    Note that all trigonometric functions use radian rather than degree for measuring angles. For function ATAN(x), x must be in (-PI/2, PI/2). For ASIN(x) and ACOS(x), x must be in [-1,1].
  • Conversion functions:
    Function Meaning Arg. Type Return Type
    INT(x) integer part x REAL INTEGER
    NINT(x) nearest integer to x REAL INTEGER
    FLOOR(x) greatest integer less than or equal to x REAL INTEGER
    FRACTION(x) the fractional part of x REAL REAL
    REAL(x) convert x to REAL INTEGER REAL
  • Other functions:
    Function Meaning Arg. Type Return Type
    MAX(x1, x2, ..., xn) maximum of x1, x2, ... xn INTEGER INTEGER
    REAL REAL
    MIN(x1, x2, ..., xn) minimum of x1, x2, ... xn INTEGER INTEGER
    REAL REAL
    MOD(x,y) remainder x - INT(x/y)*y INTEGER INTEGER
    REAL REAL

FoRtRaN: write a program to do numerical integration of any...

FoRtRaN: write a program to do numerical integration of any...: "implicit double precision (a-h,o-z) f(x)=x*x sum=0.0 write(*,*) 'x' read(*,*) x h=1/x do i=0,x sum=sum+f(i*h) enddo xsum=(..."

write a program to do numerical integration of any given function (by trapizoidal method)

implicit double precision (a-h,o-z)
 f(x)=x*x
 sum=0.0
 
 write(*,*) 'x'
 read(*,*) x
 h=1/x
 do i=0,x
      sum=sum+f(i*h)
 
 enddo
 xsum=(sum+0.5*(f(0)+f(1)))*h
 write(*,*) xsum
 
      stop
      end
 

Thursday, November 25, 2010

write a program to find the factorial of a given number (using do command)

implicit double precision (a-h,o-z)
    iprod=1
    write(*,*) 'n'
    read(*,*) n
      do i=1,n
    iprod=iprod*i
    enddo
    write(*,*) iprod
   
      stop
      end

write a program to find the factorial of a given number (using goto command)

implicit double precision (a-h,o-z)
    iprod=1
    i=1
    write(*,*) 'n'
    read(*,*) n
10    if(i.LE.n) then
    iprod=iprod*i
    i=i+1
    goto 10
    else
    write(*,*) iprod
    goto 20
20    endif
      stop
      end

write a program to find the factorial of a given number (recurrsion program)

Wednesday, November 24, 2010

write a program to display the position of given element in a array

implicit double precision (a-h,o-z)
    dimension A(11)
    DATA A/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0/
    write(*,*) 'enter p'
    read(*,*) p
    i=1
    count=1
10    if(p.EQ.A(i)) then
    count=count
      write(*,*) count
    goto 20
    else
    count=count+1
    i=i+1
    goto 10  
20    endif 
    stop
    end

write a program to sum the main diagonal of a matrices (find the trace of a matrices)

implicit double precision (a-h,o-z)
    dimension A(3,3)
    DATA A/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/
    C=0
    do i=1,3
    j=i   
    C=C+A(i,j)
    enddo
    write(*,*)C
    stop
    end

write a program to insert a given number into a kth position in array

implicit double precision (a-h,o-z)
    dimension A(10), B(11)
    DATA A/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 68.0, 8.0, 9.0, 10.0/
    write(*,*) 'enter p'
    read(*,*) p
      write(*,*) 'enter k'
    read(*,*) k
    do i=1,11
    if(i.GE.1.AND.i.lE.k-1) then
    B(i)=A(i)
    endif
    B(k)=p
    if(i.GE.k+1.AND.i.lE.11) then
    B(i)=A(i-1)
      endif
    enddo
    write(*,*) B
    stop
    end

FoRtRaN: Write a program to count even and odd number betwe...

FoRtRaN: Write a program to count even and odd number betwe...: "implicit double precision (a-h,o-z) dimension A(10), B(10) DATA A/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 68.0, 8.0, 9.0, 10.0/ count=0 ..."

Write a program to count even and odd number between a given one dimensional array

implicit double precision (a-h,o-z)
    dimension A(10), B(10)
    DATA A/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 68.0, 8.0, 9.0, 10.0/
    count=0
    do i=1,10
    B(i)=mod(A(i),2.0)
   
    if(B(i).EQ.0.0) then
    count=count+1

    else
    count=count
    endif
      enddo
    count1=10-count
    write(*,*) count, count1
    stop
    end

FoRtRaN: Program to find the root of Quadratic Equation

FoRtRaN: Program to find the root of Quadratic Equation: "implicit double precision(a-h,o-z) write(*,*) “please provide the a,b,c coeff” read(*,*) A,B,C D=B*B-4*A*C if(D.GT.0) then root1=(-B/(2*A))..."

FoRtRaN: Program to find area of a triangle whose two sides...

FoRtRaN: Program to find area of a triangle whose two sides...: "implicit double precision(a-h,o-z) write(*,*) “please provide the two sides a and b and the angle” read(*,*) A,B,theta area=A*B*SIN(the..."

FoRtRaN: Matrix Addition, Linear Combination And Multiplica...

FoRtRaN: Matrix Addition, Linear Combination And Multiplica...: "implicit double precision (a-h,o-z) dimension A(3,3), B(3,3), C(3,3),D(3,3), E(3,3) DATA A/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9..."

FoRtRaN: Write a program to determine value of Pi (pi-value...

FoRtRaN: Write a program to determine value of Pi (pi-value...: "c pi-value determination implicit double precision(a-h,o-z) sum=0.0 do i=1,1000,1 sum=sum+1/(i**2) end do pi=sqrt(6*sum) write(*,*)’the pi-v..."

FoRtRaN: Write a program to determine three given points ar...

FoRtRaN: Write a program to determine three given points ar...: "implicit double precision (a-h,o-z) write(*,*) ' enter the points first x coordinate then y coordinate' Read (*,*) x1, x2, x3, y1, y..."

FoRtRaN: write a program to invert a given matrices

FoRtRaN: write a program to invert a given matrices: "implicit double precision (a-h,o-z) dimension A(3,3), B(3,3) DATA A/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/ do i=1,3 do..."

write a program to invert a given matrices

implicit double precision (a-h,o-z)
    dimension A(3,3), B(3,3)
    DATA A/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/
    do i=1,3
    do j=1,3   
    B(i,j)=A(j,i)
    enddo
    enddo
    write(*,*)B
    stop
    end

Write a program to determine three given points are collinear or not

implicit double precision (a-h,o-z)
    write(*,*) " enter the points first x coordinate then y coordinate"
    Read (*,*) x1, x2, x3, y1, y2, y3
    s = (x1-x2)*(y2-y3)-(y1-y2)*(x2-x3)
    if(s.EQ.0) then
    write(*,*) "points are collinear"
    else
    write(*,*) "points are not collinear"
    endif
    stop
    end

Write a program to determine value of Pi (pi-value determination)

c pi-value determination
implicit double precision(a-h,o-z)
sum=0.0
do i=1,1000,1
sum=sum+1/(i**2)
end do
pi=sqrt(6*sum)
write(*,*)’the pi-value is:- ‘,pi
stop
end
c centigrade to fahrenheit conversion
implicit double precision (a-h,o-z)
dimension f(6),c(6)
write(*,*)’provide fahrenheit temp:- ‘
do i=1,6,1
read(*,*) f(i)
c(i)=5*(f(i)-32)/9
write(*,*)’the centrigrade temp is- ‘, c(i)
end do
stop
end

Matrix Addition in Fortran

click here

Matrix Addition, Linear Combination And Multiplication in Fortran

implicit double precision (a-h,o-z)
    dimension A(3,3), B(3,3), C(3,3),D(3,3), E(3,3)
    DATA A/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/
    DATA B/1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0/
    do i=1,3
    do j=1,3
    C(i,j)=A(i,j)+B(i,j)
    D(i,j)=2*A(i,j)-3*A(i,j)
   
      E(i,j)=A(i,1)*B(1,j)+A(i,2)*B(2,j)+A(i,3)*B(3,j)
    enddo
    enddo
    write(*,*)C
      write(*,*)D
    write(*,*)E
    stop
    end

Program to find area of a triangle whose two sides and an angle between them is given

implicit double precision(a-h,o-z)
write(*,*) “please provide the two sides a and b and the angle”
read(*,*) A,B,theta
area=A*B*SIN(theta)
side=DSQRT(A*A+B*B-2*A*B*COS(theta))
write(*,*) ‘area=’,area,’side=’,side
stop
END

Program to find the root of Quadratic Equation

implicit double precision(a-h,o-z)
write(*,*) “please provide the a,b,c coeff”
read(*,*) A,B,C
D=B*B-4*A*C
if(D.GT.0) then
root1=(-B/(2*A))+(SQRT(D))/(2*A)
root2=(-B/(2*A))-(SQRT(D))/(2*A)
write(*,*) root1,root2
elseif(D.EQ.0) then
root1=(-B/(2*A))
root2=root1
write(*,*) root1,root2
else
root1=(-B/(2*A))+(SQRT(-D))/(2*A)
root2=(-B/(2*A))-(SQRT(-D))/(2*A)
a=(root1+root2)/2
b=(root1-root2)/2
write(*,*) ‘realpartroot=’,a, ‘complexpartroot=’,b
endif
stop
END