NumericalMethods FortranCodes Set I

download NumericalMethods FortranCodes Set I

of 36

Transcript of NumericalMethods FortranCodes Set I

  • 7/28/2019 NumericalMethods FortranCodes Set I

    1/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 1 4/15/2013Created on September 05,2006 00:00 am

    1. Write a program to find the roots of a quadratic equation , the program should askwhether or not to re- compute for another equation and do accordingly.

    5 wr i t e( *, *) ' For t he r oot s of quadr at i c equat i on'wr i t e( *, *) ' ent er val ues of a, b, c'r ead( *, *) a, b, ci f ( b**2- 4*a*c) 1, 2, 3

    1 wr i t e( *, *) ' r eal val ue of x doesnot exi st 'got o7

    2 x=- b/ ( 2*a)wr i t e( *, *) ' t he val ue of x i s 'wr i t e( * , * )xgot o7

    3 x1=( - b+sqr t ( b**2- 4*a*c) ) / ( 2*a)x2=( - b- sqr t ( b**2- 4*a*c) ) / ( 2*a)wr i t e( *, *) ' t he val ues of x ar e'wr i t e( *, *) x1, x2

    7 wr i t e( *, *) ' Ent er 1 t o r edo & 2 t o exi t 'r ead( *, *) li f ( l . eq. 1) got o5i f ( l . eq. 2) got o6got o7

    6 st opend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    2/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 2 4/15/2013Created on September 05,2006 00:00 am

    2. Write a program to compute the values of two unknowns x1 and x2 in a system oflinear equations given the values of a1, b1, c1 and a2, b2, c2.The program shouldgenerate an error message if these equations of straight are parallel. The programshould also ask whether or not re-compute for another equation and do accordinglya1x1 +b1x1 =c1a2x1+b2x2 =c2

    c Li near Equat i ons2 wr i t e( *, *) ' Ent er t he val ues of a1, b1, c1 f or a1x1+b1x2=c1'

    r ead( *, *) a1, b1, c1wr i t e( *, *) ' Ent er t he val ues of a2, b2, c2 f or a2x1+b2x2=c2'r ead( *, *) a2, b2, c2a=a2/ a1b=b2/ b1c=c2/ c1i f ( a. eq. b) t heni f ( b. eq. c) t hengot o5el segot o6endi fel segot o6endi f

    5 wr i t e( *, *) ' The l i nes ar e par al l el t o each ot her 'got o4

    6 d=a1*b2- a2*b1x1=( b2*c1- c2*b1) / dx2=( a1*c2- a2*c1) / dwr i t e( *, *) ' The sol ut i ons ar e f ol l ows: 'wr i t e( *, 1) x1, x2

    1 f or mat ( 1x, ' x1=' , f 5. 2, / , 1x, ' x2=' , f 5. 2)4 wr i t e( *, *) ' Ent er 1 t o r edo & 2 t o exi t '

    r ead( *, *) li f ( l . eq. 1) got o2

    i f ( l . eq. 2) got o3got o4

    3 st opend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    3/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 3 4/15/2013Created on September 05,2006 00:00 am

    3. Write a program to input N number of ages and find the maximum, minimum andaverage age and to count the number of infants, children, teenagers and adultsbased on the following criteria:below 5........Infants 5 12 ................children13 19.........Teenagers 20 and above......Adults

    c Agesdi mensi on a(100)i nt eger b, c, d, ewr i t e( *, *) ' Ent er t he number of dat as'r ead( *, *) ndo 2 i =1, nwr i t e( * , 1) i

    1 f or mat ( 1x, ' Ent er ' , i 3, ' t h dat a' )read( *, *)a( i )

    2 cont i nuemax=a( 1)do 3 i =1, ni f ( a( i ) . gt . max) t henmax=a( i )el seendi f

    3 cont i nuewr i t e( *, *) ' The maxi mum age i s'

    wr i t e( *, *) maxmi n=a( 1)do 4 i =1, ni f ( mi n. gt . a( i ) ) t henmi n=a( i )el seendi f

    4 cont i nuewr i t e( *, *) ' The mi ni mum age i s'wr i t e( *, *) mi nsum=0

    do 5 i =1, nsum=sum+a( i )

    5 cont i nueavg=sum/ nwr i t e( *, *) ' The aver age age i s'wr i t e( *, *) avge=0b=0c=0d=0do 6 i =1, n

  • 7/28/2019 NumericalMethods FortranCodes Set I

    4/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 4 4/15/2013Created on September 05,2006 00:00 am

    i f ( a( i ) . l t . 5) t hen

    e=e+1el sei f ( a( i ) . l t . 13) t henb=b+1el sei f ( a( i ) . l t . 20) t henc=c+1el sed=d+1endi fendi fendi f

    6 cont i nuewr i t e( *, *) ' The number of i nf ant s i s'wr i t e( * , * )ewr i t e( *, *) ' The number of chi l dr en i s'wr i t e( * , * )bwr i t e( *, *) ' The number of t eenager s i s'wr i t e( * , * )cwr i t e( *, *) ' The number of adul t s i s'wr i t e( * , * )dwr i t e( * , *)stop

    end

  • 7/28/2019 NumericalMethods FortranCodes Set I

    5/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 5 4/15/2013Created on September 05,2006 00:00 am

    4. Write a program to reserve a given integer number using a subprogram.c Rever se of an i nt eger

    c Sub Pr ogr amsf unct i on k( n, i )k=( n- n/ 10**i *10**i ) / 10**( i - 1)returnend

    c Mai n Progr amdi mensi on a( 10)i nt eger sum, b, a, c, o, pwr i t e( *, *) ' Ent er any i nt eger number 'r ead( *, *) li =0

    1 i f ( l / 10**i . l t . 10) t heni =i +1el sei =i +1got o1endi f

    do 2 j =1, i

    a( j ) =k( l , j )2 cont i nue

    sum=0do 3 p=1, ib=a( p)c=i - po=b*10**csum=sum+o

    3 cont i nuewr i t e( *, *) ' The r ever se of t he gi ven i nt eger i s'

    wr i t e( *, *) sum

    stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    6/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 6 4/15/2013Created on September 05,2006 00:00 am

    5. Write a function subprogram to evaluate the value of f(x) where1 x

    2

    , ifx

  • 7/28/2019 NumericalMethods FortranCodes Set I

    7/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 7 4/15/2013Created on September 05,2006 00:00 am

    6.

    Write a function to convert degrees to radian value and use it to calculate theradian equivalent of 5,10,15....................175,180 degrees [180 degrees =IIradian]

    c Funct i onsf uncti on r ( x)r =x/ 180returnend

    c Mai n Progr amdo 1 i =5, 180, 5y=ia=r ( y)wr i t e( * , 2) y, a

    2 f or mat ( 1x, f 8. 2, ' D =' , f 5. 2, ' R' )1 cont i nue3 wr i t e( *, *) ' For f ur t her cal cul at i on ent er val ue i n degr ee'

    r ead( *, *) ya=r ( y)wr i t e( * , 2) y, a

    5 wr i t e( *, *) ' To r edo ent er 1 & t o exi t ent er 2'r ead( *, *) li f ( l . eq. 1) got o3

    i f ( l . eq. 2) got o4got o5

    4 st opend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    8/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 8 4/15/2013Created on September 05,2006 00:00 am

    7. Write a function to convert centigrade to Fahrenheit scale and use it to calculatethe Fahrenheit equivalent of 1,2,3,.............39,40 degrees centigrade. [F =(C * 1.8) +32]

    c 100 Fahr enhei t t o cent i gr adec Funct i ons

    f unct i on g( c)g=9*c/ 5. +32returnend

    c Mai n Progr amdo 1 i =1, 40, 1c=if =g( c)wr i t e( * , 3) c, f

    3 f ormat ( 1x, f 7. 2, ' C =' , f 7. 2, ' F' )1 cont i nue6 wr i t e( *, *) ' For f ur t her cal cul at i on cal cul at i on ent er 1 & t o exi t

    *ent er 2'r ead( *, *) ni f ( n. eq. 1) got o4i f ( n. eq. 2) got o5got o6

    4 wr i t e( *, *) ' Ent er val ue i n cel ci us'

    r ead( *, *) cf =g( c)wr i t e( * , 3) c, f got o6

    5 st opend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    9/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 9 4/15/2013Created on September 05,2006 00:00 am

    8. Write a program to calculate mean and standard deviation from N number of data.Mean =x / n SD = sqrt(x / n (x / n )

    2

    )

    c Mean & Standar d Devi at i oni nt eger sum1, sum2wr i t e( *, *) ' Ent er number s of t er ms'r ead( *, *) nsum1=0sum2=0do 1 l =1, n, 1wr i t e( * , 2) l

    2 f or mat ( 1x, ' Ent er ' , i 3, ' t h t er m' )r ead( *, *) msum1=sum1+msum2=sum2+m**2

    1 cont i nueMean=sum1/ nvar =sum2sd=sqr t ( var / n- ( sum1/ n) **2)wr i t e( *, 3) Mean, sd

    3 f or mat ( 1x, ' Mean =' , i 5, / , 1x, ' St andar d Devi at i on =' , f 7. 2)stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    10/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 10 4/15/2013Created on September 05,2006 00:00 am

    9. Write a program to find out whether a given square matrix is symmetric[aij =aji for all elements]

    c Symmet r i c Mat r i xDi mensi on a( 10, 10)wr i t e( *, *) ' Ent er t he or der of squar e mat r i x'r ead( *, *) kwr i t e( *, *) ' Ent er t he t er ms of t he mat r i x'do 1 i =1, k, 1wr i t e( * , 4) i

    4 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' )read( * , * ) ( a( i , j ) , j =1, k)

    1 cont i nuen=0do 2 i =1, k, 1do 3 j =1, k, 1i f ( a( i , j ) . eq. a( j , i ) ) t henel sen=n+1endi f

    3 cont i nue2 cont i nue

    i f ( n. eq. 0) t henwr i t e( *, *) ' The gi ven mat r i x i s symmet r i c'

    el sewr i t e( *, *) ' The gi ven mat r i x i s not symmet r i c'endi fstopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    11/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 11 4/15/2013Created on September 05,2006 00:00 am

    10. Write a program to find out whether a given square matrix is skew symmetric or

    not.[aij =-aji for all non-diagonal elements and all diagonal elements must be zero]

    c Skew- Symmet r i c Mat r i xDi mensi on a( 10, 10)wr i t e( *, *) ' Ent er t he or der of squar e mat r i x'r ead( *, *) kwr i t e( *, *) ' Ent er t he t er ms of t he mat r i x'do 1 i =1, k, 1wr i t e( * , 4) i

    4 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' )read( * , * ) ( a( i , j ) , j =1, k)

    1 cont i nuen=0do 2 i =1, k, 1do 3 j =1, k, 1i f ( i . eq. j ) t heni f ( a( i , j ) . eq. 0) got o3n=n+1got o3endi fi f ( a( i , j ) . eq. - a( j , i ) ) t henel se

    n=n+1endi f

    3 cont i nue2 cont i nue

    i f ( n. eq. 0) t henwr i t e( *, *) ' The gi ven mat r i x i s skew symmet r i c. 'el sewr i t e( *, *) ' The gi ven mat r i x i s not skew symmet r i c. 'endi fstopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    12/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 12 4/15/2013Created on September 05,2006 00:00 am

    11. Write a program to input two square matrices and add the first matrix with the

    transpose of the second matrix.

    c Addi t i on wi t h t r anspose of ot herdi mensi on a( 10, 10) , c( 10, 10) , d( 10, 10) , e( 10, 10)

    wr i t e( *, *) ' For t he addi t i on of mat r i xes'

    wr i t e( *, *) ' Ent er t he or der f or 1st mat r i x as r , c'r ead( *, *) m, n

    wr i t e( *, *) ' Ent er t he or der f or 2nd mat r i x as r , c'read( *, *)k, l

    i f ( m. eq. l ) t heni f ( n. eq. k) t hengot o11el seendi fel seendi fwr i t e( *, *) ' The mat r i x addi t i on i s not def i ned'stop

    11 wr i t e( *, *) ' Ent er t he val ues of t he t er ms of 1st mat r i x'do 1 i =1, m, 1wr i t e( * , 2) i

    2 f or mat ( 1x, ' Ent er t er ms i n' , i 2, ' t h r ow' )read( * , * ) ( a( i , j ) , j =1, n)

    1 cont i nue

    wr i t e( *, *) ' Ent er t he val ues of t he t er ms of 2nd mat r i x'do 12 i =1, kwr i t e( *, 20) i

    20 f or mat ( 1x, , ' Ent er t er ms i n ' , i 2, ' t h r ow' )

    r ead( * , * ) ( c( i , j ) , j =1, l )12 cont i nue

    do 13 i =1, ldo 14 j =1, kd( i , j ) =c( j , i )

    14 cont i nue13 cont i nue

    do 5 i =1, m, 1do 5 j =1, n, 1

  • 7/28/2019 NumericalMethods FortranCodes Set I

    13/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 13 4/15/2013Created on September 05,2006 00:00 am

    e( i , j ) =a( i , j ) +d( i , j )

    5 cont i nue

    wr i t e( * , *) ' The f i rst mat r i x i s f ol l ows'do 6 i =1, mwr i te( * , * ) ( a( i , j ) , j =1, n)

    6 cont i nue

    wr i t e( *, *) ' The second mat r i x i s f ol l ows'do 8 i =1, kwr i t e( * , *) ( c( i , j ) , j =1, l )

    8 cont i nue

    wr i t e( *, *) ' The t r anspose of second mat r i x i s f ol l ows'do 15 i =1, lwr i te( * , * ) ( d( i , j ) , j =1, k)

    15 cont i nue

    wr i t e( *, *) ' The sum mat r i x i s f ol l ows'do 10 i =1, mwr i te( * , * ) ( e( i , j ) , j =1, n)

    10 cont i nue

    stop

    end

    12. Write a program to compute the sum of the following series up to n terms orsignificant up to 6 decimal places:

    sinx =x x3/3! +x5/5! x7/7! +..............

    c Sum of Ser i es5 wr i t e( *, *) ' To ent er number of t er ms ent er 1 or 2 f or sum

    upt o si x

    *deci mal pl aces'r ead( *, *) ni f ( n. eq. 1) t henwr i t e( *, *) ' Ent er number of t er ms'r ead( *, *) lel sei f ( n. eq. 2) t hengot o4el segot o5endi f

  • 7/28/2019 NumericalMethods FortranCodes Set I

    14/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 14 4/15/2013Created on September 05,2006 00:00 am

    endi f

    4 wr i t e( *, *) ' Ent er val ue of x i n degr ees'r ead( *, *) xx=( x*22/ 7. ) / 180sum=xt er m=xm=1i =0

    2 i =i +2ai =it er m=- t er m*x*x/ ( ai *( ai +1) )i f ( n. eq. 1) t henm=m+1

    i f ( m. gt . l ) t hengot o1el sesum=sum+t er mgot o2endi fel sei f ( abs( t er m) . l t . 0. 000001) t hengot o1el sesum=sum+t er m

    got o2endi fendi f

    1 wr i t e( *, 3) sum3 f or mat ( 3x, ' Si nx=' , f 15. 10)

    stopend

    13. Write a program to compute the sum of the following series up to n terms or

    significant up to 6 decimal places:cosx =1 x2/2! +x4/4! x6/6! +.............

    c Sum of Ser i es f or cosx5 wr i t e( *, *) ' To ent er number of t er ms ent er 1 or 2 f or sum

    upt o si x*deci mal pl aces'r ead( *, *) ni f ( n. eq. 1) t henwr i t e( *, *) ' Ent er number of t er ms'r ead( *, *) l

  • 7/28/2019 NumericalMethods FortranCodes Set I

    15/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 15 4/15/2013Created on September 05,2006 00:00 am

    el se

    i f ( n. eq. 2) t hengot o4el segot o5endi fendi f

    4 wr i t e( *, *) ' Ent er val ue of x i n degr ees'r ead( *, *) xx=( x*22/ 7. ) / 180sum=1t er m=1m=1

    i =- 12 i =i +2

    ai =it er m=- t er m*x*x/ ( ai *( ai +1) )i f ( n. eq. 1) t henm=m+1i f ( m. gt . l ) t hengot o1el sesum=sum+t er mgot o2

    endi fel sei f ( abs( t er m) . l t . 0. 000001) t hengot o1el sesum=sum+t er mgot o2endi fendi f

    1 wr i t e( *, 3) sum3 f or mat ( 3x, ' Cosx=' , f 15. 10)

    stopend

    14. Write a program to compute the sum of the following series up to n terms orsignificant up to 6 decimal places:

    ex=1 +x/1! +x2/2! +x3/3 +..............

    c Sum of ser i es f or exponent i al5 wr i t e( *, *) ' To ent er number of t er ms ent er 1 or 2 f or sum

    upt o si x

  • 7/28/2019 NumericalMethods FortranCodes Set I

    16/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 16 4/15/2013Created on September 05,2006 00:00 am

    *deci mal pl aces'

    r ead( *, *) ni f ( n. eq. 1) t henwr i t e( *, *) ' Ent er number of t er ms'r ead( *, *) lel sei f ( n. eq. 2) t hengot o4el segot o5endi fendi f

    4 wr i t e( *, *) ' Ent er val ue of x'

    r ead( *, *) xsum=1t er m=1m=1i =0

    2 i =i +1ai =it er m=t er m*x/ aii f ( n. eq. 1) t henm=m+1i f ( m. gt . l ) t hen

    got o1el sesum=sum+t er mgot o2endi fel sei f ( t er m. l t . 0. 000001) t hengot o1el sesum=sum+t er mgot o2

    endi fendi f

    1 wr i t e( *, 3) sum3 f or mat ( 3x, ' e**x=' , f 25. 10)

    stopend

    15. Write a program to compute the sum of the following series up to n terms orsignificant up to 6 decimal places:

    e-x =1 x/1! +x2/! x3/3! +............

  • 7/28/2019 NumericalMethods FortranCodes Set I

    17/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 17 4/15/2013Created on September 05,2006 00:00 am

    c Sum of ser i es f or negat i ve power ed exponent i al5 wr i t e( *, *) ' To ent er number of t er ms ent er 1 or 2 f or sumupt o si x

    *deci mal pl aces'r ead( *, *) ni f ( n. eq. 1) t henwr i t e( *, *) ' Ent er number of t er ms'r ead( *, *) lel sei f ( n. eq. 2) t hengot o4el segot o5endi fendi f

    4 wr i t e( *, *) ' Ent er val ue of x'r ead( *, *) xx=abs( x)sum=1t er m=1m=1i =0

    2 i =i +1

    ai =it erm=- t erm*x/ aii f ( n. eq. 1) t henm=m+1i f ( m. gt . l ) t hengot o1el sesum=sum+t er mgot o2endi fel se

    i f ( abs( t er m) . l t . 0. 000001) t hengot o1el sesum=sum+t er mgot o2endi fendi f

    1 wr i t e( *, 3) sum3 f or mat ( 3x, ' e**x=' , f 25. 10)

    stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    18/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 18 4/15/2013Created on September 05,2006 00:00 am

    16. Write a program to compute the sum of the following series up to n terms orsignificant up to 6 decimal places:log(1 +x) =1 x +x2/2 x3/3 +x4/4 - ................

    c Sum of ser i es f or l og5 wr i t e( *, *) ' To ent er number of t er ms ent er 1 or 2 f or sum

    upt o si x*deci mal pl aces'r ead( *, *) ni f ( n. eq. 1) t henwr i t e( *, *) ' Ent er number of t er ms'r ead( *, *) lel sei f ( n. eq. 2) t hengot o4el segot o5endi fendi f

    4 wr i t e( *, *) ' Ent er val ue of x'r ead( *, *) xx=abs( x)sum=1- x

    t er m=- xm=2i =1

    2 i =i +1ai =it er m=- t er m*x*( ai - 1) / aii f ( n. eq. 1) t henm=m+1i f ( m. gt . l ) t hengot o1el se

    sum=sum+t er mgot o2endi fel sei f ( abs( t er m) . l t . 0. 000001) t hengot o1el sesum=sum+t er mgot o2endi fendi f

  • 7/28/2019 NumericalMethods FortranCodes Set I

    19/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 19 4/15/2013Created on September 05,2006 00:00 am

    1 wr i t e( *, 3) sum

    3 f or mat ( 3x, ' l og( 1+x) =' , f 25. 10)stopend

    17. Write a function subprogram to calculate the factorial of a number and use it tocalculate the value ofnCr

    nC r =n!/r!(n r)!

    c nCrc Funct i ons

    f uncti on f ( k)f =1do 1 i =1, kf =f *i

    1 cont i nuereturnend

    c Mai n Progr amwr i t e( *, *) ' Ent er val ues of n & r f or nCr 'r ead( *, *) n, lm=f (n) / ( f ( l ) * f (n- l ) )wr i te( * , 2)n, l , m

    2 f or mat ( 1x, ' C( ' , i 2, ' , ' , i 2, ' ) = ' , i 3)

    stopend

    18. Write a program to multiply two matrices of order M * N and N * Lc Mat r i x Mul t i pl i cat i on

    Di mensi on a(10, 10) , b( 10, 10) , c( 10, 10)i nt eger o

    wr i t e( *, *) ' Ent er t he or der of 1st mat r i x i n t he f or m r , c'

    read( *, *)k, lwr i t e( *, *) ' Ent er t he or der of mat r i x i n t he f or m r , c'r ead( *, *) m, n

    i f ( l . ne. m) t henwr i t e( *, *) ' The mat r i x mul t i pl i cat i on i s not def i ned'got o99endi f

    wr i t e( *, *) ' Ent er t he t er ms of t he mat r i x'do 1 i =1, k

  • 7/28/2019 NumericalMethods FortranCodes Set I

    20/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 20 4/15/2013Created on September 05,2006 00:00 am

    wri t e( *, 2) i

    2 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' )r ead( * , * ) ( a( i , j ) , j =1, l )1 cont i nue

    wr i t e( *, *) ' Ent er t he t er ms of 2nd t he mat r i x'do 3 i =1, mwri t e( *, 4) i

    4 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' )read( * , * ) ( b( i , j ) , j =1, n)

    3 cont i nue

    do 5 i =1, kdo 6 j =1, nc( i , j ) =0do 7 o=1, lc( i , j ) =c( i , j ) +a( i , o) *b( o, j )

    7 cont i nue6 cont i nue5 cont i nue

    wr i t e( *, *) ' The pr oduct mat r i x i s'do 8 i =1, k

    wr i te( * , 9) ( c( i , j ) , j =1, n)9 f or mat ( 1x, 100f 5. 2)8 cont i nue

    99 st opend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    21/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 21 4/15/2013Created on September 05,2006 00:00 am

    19. Write a program to compute the determinant of a 3 * 3 matrix.c Mat r i x Mul t i pl i cat i on

    Di mensi on a( 3, 3)

    wr i t e( *, *) ' Ent er t he t er ms of t he 3 X 3 mat r i x'do 1 i =1, 3wr i t e( * , 2) i

    2 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' )read( * , * ) ( a( i , j ) , j =1, 3)

    1 cont i nue

    d=a( 1, 1) *( a( 2, 2) *a( 3, 3) - a( 3, 2) *a( 2, 3) ) +a( 1, 2) *( a( 2, 3)*a( 3, 1) - a( 3, 3

    *) *a( 2, 1) ) +a( 1, 3) *( a( 2, 1) *a( 3, 2) - a( 3, 1) *a( 2, 2) )

    wr i t e( *, *) ' The det er mi nant of t he mat r i x i s'wr i t e( * , 3) d

    3 f or mat ( 1x, f 9. 2)cont i nue

    stopend

    20. Write a program to generate all the leap years within a given range.[A year is a leap year if it is exactly divisible by 4 with an exception that if it isdivisible by 100 it must also be divisible by 400. example : 1996 and 1600 areleap years but 1900 is not.]

    c Leap yearswr i t e( *, *) ' Ent er t he r ange of year s f or t he l eap year 'r ead( *, *) m, nwr i t e( *, *) ' The l eap year s wi t hi n t he gi ven r ange ar e : 'do 1 i =m, ni f ( mod( i , 4) . eq. 0) t hen

    i f ( mod( i , 100) . eq. 0) t heni f ( mod( i , 400) . eq. 0) t henwr i t e( * , * ) iendi fel sewr i t e( * , * ) iendi fendi f

    1 cont i nuestopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    22/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 22 4/15/2013Created on September 05,2006 00:00 am

    21.Write a program to arrange an array of arbitrary data into ascending order.

    c Ascendi ng orderi nt eger cdi mensi on a( 100) , b( 100)wr i t e( *, *) ' Ent er number of t er ms'r ead( *, *) ndo 1 i =1, n, 1wr i t e( * , 2) i

    2 f or mat ( 1x, ' Ent er ' , i 3, ' t h t er m' )read( *, *)a( i )

    1 cont i nue

    do 3 i =1, n- 1do 3 j =i +1, ni f ( a( i ) . gt . a( j ) ) t hent =a( i )a( i ) =a( j )a( j ) =tendi f

    3 cont i nuewr i t e( *, *) ' The t er ms i n ascendi ng or der i s : 'wr i t e( * , 4) ( a( i ) , i =1, n)

    4 f or mat ( 1x, 100f 7. 2)

    stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    23/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 23 4/15/2013Created on September 05,2006 00:00 am

    22. Write a program to input a matrix of given dimension and compute the sum of

    any given row.

    c SumDi mensi on a( 10, 10) , b( 100)wr i t e( *, *) ' Ent er t he or der of mat r i x i n t he f or m r , c'read( *, *)k, lwr i t e( *, *) ' Ent er t he t er ms of t he mat r i x'do 1 i =1, k, 1wr i t e( * , 4) i

    4 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' )r ead( * , * ) ( a( i , j ) , j =1, l )

    1 cont i nue

    wr i t e( *, *) ' Ent er t he r ow t o cal cul at e the sum'r ead( *, *) isum=0do 3 j =1, l , 1sum=sum+a( i , j )

    3 cont i nuewr i t e( *, *) ' The sum of el ement s of gi ven r ow i s : 'wr i t e( *, 5) sum

    5 f or mat ( 1x, f 5. 2)stop

    end

  • 7/28/2019 NumericalMethods FortranCodes Set I

    24/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 24 4/15/2013Created on September 05,2006 00:00 am

    23. Write a program to input a matrix of given dimension and compute the sum ofany given column.

    c SumDi mensi on a( 10, 10) , b( 100)wr i t e( *, *) ' Ent er t he or der of mat r i x i n t he f or m r , c'read( *, *)k, lwr i t e( *, *) ' Ent er t he t er ms of t he mat r i x'do 1 i =1, k, 1wr i t e( * , 4) i

    4 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' )r ead( * , * ) ( a( i , j ) , j =1, l )

    1 cont i nue

    wr i t e( *, *) ' Ent er t he col umn t o cal cul at e t he sum'r ead( *, *) jsum=0do 3 i =1, l , 1sum=sum+a( i , j )

    3 cont i nuewr i t e( *, *) ' The sum of el ement s of gi ven col umn i s : 'wr i t e( *, 5) sum

    5 f or mat ( 1x, f 5. 2)

    stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    25/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 25 4/15/2013Created on September 05,2006 00:00 am

    24. Write a program to input a matrix of given dimension and find the maximum

    value along with its position.(row and column)

    c SumDi mensi on a( 10, 10)r eal max1, max2

    wr i t e( *, *) ' Ent er t he or der of mat r i x i n t he f or m r , c'read( *, *)k, lwr i t e( *, *) ' Ent er t he t er ms of t he mat r i x'do 1 i =1, k, 1wr i t e( * , 4) i

    4 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' )r ead( * , * ) ( a( i , j ) , j =1, l )

    1 cont i nue

    wr i t e( *, *) ' Ent er t he or der of el ement t o f i nd mi n. andmax. number

    * al ong wi t h i t s posi t i on'r ead( *, *) m, n

    max1=a( m, n)do 3 j =1, li f ( max1. l t . a( m, j ) ) t hen

    max1=a( m, j )endi f

    3 cont i nue

    max2=a( m, n)do 6 j =1, ki f ( max2. l t . a( j , n) ) t henmax2=a( j , n)endi f

    6 cont i nue

    wr i t e( *, *) ' The max. val ue al ong i t s r ow i s : 'wr i t e( *, 8) max1

    8 f or mat ( 1x, f 5. 2)wr i t e( *, *) ' The max. val ue al ong i t s col umn i s : 'wr i t e( *, 8) max2

    stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    26/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 26 4/15/2013Created on September 05,2006 00:00 am

    25. Write a program to input an array of N elements and find the minimum and maxi-

    mum values along with their position.

    c Mi nmaxDi mensi on a( 100)r eal max1, mi n1

    wr i t e( *, *) ' Ent er number of t er ms'r ead( *, *) kwr i t e( *, *) ' Ent er t he t er ms of t he mat r i x'do 1 i =1, k, 1wr i t e( * , 4) i

    4 f or mat ( 1x, ' Ent er ' , i 2, ' t h ter m' )read( *, *)a( i )

    1 cont i nue

    max1=a( 1)do 3 j =1, ki f ( max1. l t . a( j ) ) t henmax1=a( j )endi f

    3 cont i nue

    mi n1=a( 1)

    do 6 j =1, ki f ( mi n1. ge. a( j ) ) t henmi n1=a( j )endi f

    6 cont i nue

    wr i t e( *, *) ' The max. val ue i s : 'wr i t e( *, 8) max1

    8 f or mat ( 1x, f 5. 2)wr i t e( *, *) ' The mi n val ue i s : 'wr i t e( *, 8) mi n1

    stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    27/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 27 4/15/2013Created on September 05,2006 00:00 am

    26. Write a program to input a matrix of given dimension and find the minimum

    value along with its position.(row and column)

    c Mi nDi mensi on a( 10, 10)r eal mi n1, mi n2

    wr i t e( *, *) ' Ent er t he or der of mat r i x i n t he f or m r , c'read( *, *)k, lwr i t e( *, *) ' Ent er t he t er ms of t he mat r i x'do 1 i =1, k, 1wr i t e( * , 4) i

    4 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' )r ead( * , * ) ( a( i , j ) , j =1, l )

    1 cont i nue

    wr i t e( *, *) ' Ent er t he or der of el ement t o f i nd mi n. number* al ong wi t h i t s posi t i on'r ead( *, *) m, n

    mi n1=a( m, n)do 5 j =1, l , 1i f ( mi n1. ge. a( m, j ) ) t henmi n1=a( m, j )

    endi f5 cont i nue

    mi n2=a( m, n)do 7 j =1, k, 1i f ( mi n2. ge. a( j , n) ) t henmi n2=a( j , n)endi f

    7 cont i nue

    8 f or mat ( 1x, f 5. 2)

    wr i t e( *, *) ' The mi n. val ue al ong i t s r ow i s : 'wr i t e( *, 8) mi n1

    wr i t e( *, *) ' The mi n. val ue al ong i t s col umn i s : 'wr i t e( *, 8) mi n2

    stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    28/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 28 4/15/2013Created on September 05,2006 00:00 am

    27. Write a program to input square matrix and find the sum of all the non-diagonal

    elements.

    c SumDi mensi on a( 10, 10)wr i t e( *, *) ' Ent er t he or der of squar e mat r i x'r ead( *, *) kwr i t e( *, *) ' Ent er t he t er ms of t he mat r i x'do 1 i =1, k, 1wr i t e( * , 4) i

    4 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' )read( * , * ) ( a( i , j ) , j =1, k)

    1 cont i nuesum=0do 2 i =1, k, 1do 3 j =1, k, 1i f ( i . eq. j ) t henel sesum=sum+a( i , j )endi f

    3 cont i nue2 cont i nue

    wr i t e( *, *) ' The sum of non- di agonal el ement s of mat r i x i s'wr i t e( *, *) sum

    stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    29/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 29 4/15/2013Created on September 05,2006 00:00 am

    28. Write a program to input an integer array of N elements and display and find the

    sum of all the elements which are exactly divisible by 5 but not by 7.

    c Di vi si bl e by 5 but not by 7i nt eger adi mensi on a(100)

    wr i t e( *, *) ' Ent er number of t er ms'r ead( *, *) n

    wr i t e( *, *) ' Ent er t he t er ms'read( *, *) ( a( i ) , i =1, n)

    sum=0do 1 i =1, ni f ( mod( a( i ) , 5) . eq. 0) t heni f ( mod( a( i ) , 7) . ne. 0) t henwr i te( * , * )a( i )sum=sum+a( i )el seendi fel seendi f

    1 cont i nue

    wr i t e( *, *) ' The sum of t he r esul t i ng t er ms i s : 'wr i t e( *, 2) sum

    2 f or mat ( 1x, f 5. 2)stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    30/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 30 4/15/2013Created on September 05,2006 00:00 am

    29. Write a program to input an integer array of N elements and display and find the

    sum of all the elements which are exactly divisible by 7 but not by 5.

    c Di vi si bl e by 7 but not by 5i nt eger adi mensi on a(100)

    wr i t e( *, *) ' Ent er number of t er ms'r ead( *, *) n

    wr i t e( *, *) ' Ent er t he t er ms'read( *, *) ( a( i ) , i =1, n)

    sum=0do 1 i =1, ni f ( mod( a( i ) , 7) . eq. 0) t heni f ( mod( a( i ) , 5) . ne. 0) t henwr i te( * , * )a( i )sum=sum+a( i )el seendi fel seendi f

    1 cont i nue

    wr i t e( *, *) ' The sum of t he r esul t i ng t er ms i s : 'wr i t e( *, 2) sum

    2 f or mat ( 1x, f 5. 2)stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    31/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 31 4/15/2013Created on September 05,2006 00:00 am

    30. Write a subroutine to calculate the base area, total surface area, and volume of the

    cylinder and use it for at least 3 sets of data in the main program and display theresult.

    c Sub r out i nesubr out i ne ar ea( r , l , b, c, t , v)b=22/ 7. *r *rc=2*22/ 7. *r *lt =b+cv=b*lreturnend

    c Mai n pr ogr amdo 1 i =1, 3wr i t e( * , 2) i

    2 f ormat ( 1x, ' Ent er ' , i 2, ' t h set of ( r , l ) ' )read( * , * ) r , lwr i t e( * , 3) i

    3 f or mat ( 1x, ' The r esul t s f or ' , i 2, ' set of dat a ar e : ' )cal l ar ea( r , l , b, c, t , v)wr i t e( *, *) ' The ar ea of t he base i s'wr i t e( * , * )bwr i t e( *, *) ' The ar ea of t he cur ved sur f ace i s'

    wr i t e( * , * )cwr i t e( *, *) ' The t ot al ar ea i s 'wr i t e( * , * ) twr i t e( *, *) ' The vol ume of t he cyl i nder i s'wr i t e( * , * )v

    1 cont i nuestopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    32/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 32 4/15/2013Created on September 05,2006 00:00 am

    31. Write a program to input three sides of a triangle(the longest side being unknown)

    and check whether it is a right angled triangle or not. [Hint : use Pythagorastheorem h2 =p2 +b2 ]

    c Ri ght angl ed t r i angl edi mensi on a( 3)wr i t e( *, *) ' Ent er t he si des of t he t r i angl e'read( *, *) ( a( j ) , j =1, 3)max=0do 1 i =1, 3i f ( a( i ) . gt . max) t henmax=a( i )el seendi f

    1 cont i nues=0do 2 i =1, 3i f ( a( i ) . ne. max) t hens=s+a( i ) **2el seendi f

    2 cont i nuer =sqr t ( s)i f ( max. eq. r ) t hen

    wr i t e( *, *) ' The t r i angl e i s r i ght angl ed. 'el sewr i t e( *, *) ' The t r i angl e i s not r i ght angl ed. 'endi fstopend

    32. Write a program to generate the square roots of all the integer numbers within agiven range.

    c Square Root

    wr i t e( *, *) ' Ent er r ange f or t he i nt er ger s'read( * , * ) l , mdo 1 i =l , m, 1a=is=sqr t ( a)wr i te( * , 2) i , s

    2 f ormat ( 1x, ' sqr t ( ' , i 3, ' ) =' , f 7. 2)1 cont i nue

    stopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    33/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 33 4/15/2013Created on September 05,2006 00:00 am

    33. Write a program to generate all the prime numbers within a given range.c Pr i me Number s

    wr i t e( *, *) ' Ent er r ange f or t he pr i me number s'read( * , * ) l , mi =0n=l

    1 k=12 k=k+1

    i f ( n. eq. 1) t henn=n+1got o1el seendi fi f ( k. gt . n/ 2. ) t heni =i +1wr i te( * , 7) i , n

    7 f or mat ( 2x, i 3, ' ) ' , 2x, i 10)i f ( n. gt . m) got o9n=n+1got o1el sei f ( n- n/ k*k. eq. 0) t henn=n+1

    i f ( n. gt . m) got o9got o1el segot o2endi fendi f

    9 st opend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    34/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 34 4/15/2013Created on September 05,2006 00:00 am

    34. Write a function subprogram to calculate simple interest and use it in the main

    program to calculate the simple interest from given principal amount and interestrate for 1,2,3,........25 years.

    c Si mpl e I nt er estc Funct i ons

    f unct i on f ( x, y, n)f =x*y*n/ 100returnend

    c Mai n Progr amwr i t e( *, *) ' Ent er Pr i nci pal Amount 'r ead( *, *) awr i t e( *, *) ' Ent er I nt er est Rat e'r ead( *, *) bdo 1 l =1, 25d=f ( a, b, l )wr i t e( * , 2) l

    2 f ormat ( 1x, ' The t ot al i nt erest f or ' , i 3, ' years i s ' )wr i t e( * , * )d

    1 cont i nuestopend

    35. Write a program to find the sum of the following series:1 * 2 +3 * 5 +5 * 8 +7 * 11 +...........up to N terms

    c Ser i esi nt eger sumwr i t e( *, *) ' Ent er number of t er ms t o be added'r ead( *, *) nk=- 1l =- 1sum=0

    do 1 i =1, nk=k+2l =l +3sum=sum+( k) *( l )

    1 cont i nuewr i t e( *, *) ' The sum of t he ser i es i s 'wr i t e( *, *) sumstopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    35/36

    SARAT

    FORTRAN @ CIVIL I /II

    All Page 35 4/15/2013Created on September 05,2006 00:00 am

    36. Write a program to find the sum of the following series:

    1 * 2 +4 * 4 +7 * 6 +10 * 8 +...........up to N terms

    c Ser i esi nt eger sumwr i t e( *, *) ' Ent er number of t er ms t o be added'r ead( *, *) nk=- 2l =0sum=0do 1 i =1, nk=k+3l =l +2sum=sum+( k) *( l )

    1 cont i nuewr i t e( *, *) ' The sum of t he ser i es i s 'wr i t e( *, *) sumstopend

    37. Write a program to find the sum of the following series:1 * 2 +4 * 6 +7 * 10 +10 * 14 +........up to N terms

    c Ser i esi nt eger sumwr i t e( *, *) ' Ent er number of t er ms t o be added'r ead( *, *) nk=- 2l =- 2sum=0do 1 i =1, nk=k+3l =l +4sum=sum+( k) *( l )

    1 cont i nuewr i t e( *, *) ' The sum of t he ser i es i s 'wr i t e( *, *) sumstopend

  • 7/28/2019 NumericalMethods FortranCodes Set I

    36/36

    SARAT

    FORTRAN @ CIVIL I /II

    All P 36 4/15/2013

    38. Write a program to generate the multiplication table of a given number within a

    given range.

    c Mul t i pl i cat i on t abl ewr i t e( *, *) ' Ent er t he number t o mul t i pl y, number t o bemul t i pl i ed f r

    *om and number up t o be mul t i pl i ed wi t h'read( * , * )n, l , mdo 1 i =l , m, 1k=n*iwr i te( * , 2)n, i , k

    2 f ormat ( 1x, i 5, 3x, ' X' , i 5, ' =' , i 8)1 cont i nue

    stopend