SARAT
F ORTRAN RTRAN @ CIVI L I /I I
1. Write a program to find the roots of a quadratic equation , the program should ask whether or not to re- compute for another equation and do accordingly. 5
1 2
3
7
6
All
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 of a, b, c ' r ead( * , * ) a, b, c i f ( b* * 22- 4* a* a* c) c) 1, 2, 3 wr i t e( *, *) ' r eal val ue of x doesn sno ot ex exi st ' got o7 x=- b/ ( 2*a) wr i t e( * , * ) ' t he val ue of x i s ' wr i t e( * , * ) x got o7 x x1 1=( - b+sq sqrr t ( b** 2- 4*a*c) ) / ( 2*a) x2=( - b- sq sqrr t ( b**2**2- 4*a*c) *a*c) ) / ( 2*a) *a) wr i t e( * , * ) ' t he val ues of of x ar e' wr i t e( * , * ) x1, x2 wr i t e( *, *) ' Ent er 1 t o r edo & 2 t o exi t ' r ead( * , * ) l i f ( l . eq. 1) got o5 i f ( l . eq. 2) got o6 got o7 st op end
Page Page 1 Created on September 05,2006 00:00 00:00 am
4/15/201 4/15/2013 3
SARAT
F ORTRAN RTRAN @ CIVI L I /I I
2. Write a prog progrr am to compute compute the value values s of of two unknowns unknowns x1 and x2 in a system of linear equations given the values of a1, b1, c1 and a2, b2, c2.The .T he prog pr ogrr am shoul should d gener nerate an error message ssageif the these seequations quati ons of strai straight ght are parall parallel. The T he prog pr ogrram should also ask whether or not re-compute for another equation and do accordingly a1x1 +b1x1 =c1 a2x1 +b2x2 =c2 c 2
5 6
1 4
3
All
Li near Equat i ons ons wr i t e( *, *) ' Ent er t he val val ues of of a1, b1, c1 f or a1x1 a1x1+ +b1x2= x2=c1' c1' r ead( *, *) a1, b1, c1 wr i t e( *, *) ' Ent er t he val val ues of a2, b2, c2 f or a2x1+ x1+b2x2= x2=c2' c2' r ead( *, *) a2, b2, c2 a=a2/ a1 b=b2/ b1 c=c2/ c1 i f ( a. eq. b) t hen i f ( b. eq. c ) t hen got o5 el s e got o6 endi f el s e got o6 endi f wr i t e( *, *) ' The l i nes ar e par al l el t o each ot her ' got o4 d=a1*b2a1* b2- a2*b1 a2* b1 x1= x1=( b2*c1- c2*b1 c2*b1)) / d x2= x2=( a1*c2a1*c2- a2*c a2*c 1) / d wr i t e( * , * ) ' The s ol ol ut i ons ar ar e f ol l ows : ' wr i t e( * , 1) x1, x2 f or mat ( 1x, ' x1=' , f 5. 2, / , 1x, ' x2=' , f 5. 2) wr i t e( *, *) ' Ent er 1 t o r edo & 2 t o exi t ' r ead( * , * ) l i f ( l . eq. 1) got o2 i f ( l . eq. 2) got o3 got o4 st op end
Page Page 2 Created on September 05,2006 00:00 00:00 am
4/15/201 4/15/2013 3
SARAT
F ORTRAN RTRAN @ CIVI L I /I I
2. Write a prog progrr am to compute compute the value values s of of two unknowns unknowns x1 and x2 in a system of linear equations given the values of a1, b1, c1 and a2, b2, c2.The .T he prog pr ogrr am shoul should d gener nerate an error message ssageif the these seequations quati ons of strai straight ght are parall parallel. The T he prog pr ogrram should also ask whether or not re-compute for another equation and do accordingly a1x1 +b1x1 =c1 a2x1 +b2x2 =c2 c 2
5 6
1 4
3
All
Li near Equat i ons ons wr i t e( *, *) ' Ent er t he val val ues of of a1, b1, c1 f or a1x1 a1x1+ +b1x2= x2=c1' c1' r ead( *, *) a1, b1, c1 wr i t e( *, *) ' Ent er t he val val ues of a2, b2, c2 f or a2x1+ x1+b2x2= x2=c2' c2' r ead( *, *) a2, b2, c2 a=a2/ a1 b=b2/ b1 c=c2/ c1 i f ( a. eq. b) t hen i f ( b. eq. c ) t hen got o5 el s e got o6 endi f el s e got o6 endi f wr i t e( *, *) ' The l i nes ar e par al l el t o each ot her ' got o4 d=a1*b2a1* b2- a2*b1 a2* b1 x1= x1=( b2*c1- c2*b1 c2*b1)) / d x2= x2=( a1*c2a1*c2- a2*c a2*c 1) / d wr i t e( * , * ) ' The s ol ol ut i ons ar ar e f ol l ows : ' wr i t e( * , 1) x1, x2 f or mat ( 1x, ' x1=' , f 5. 2, / , 1x, ' x2=' , f 5. 2) wr i t e( *, *) ' Ent er 1 t o r edo & 2 t o exi t ' r ead( * , * ) l i f ( l . eq. 1) got o2 i f ( l . eq. 2) got o3 got o4 st op end
Page Page 2 Created on September 05,2006 00:00 00:00 am
4/15/201 4/15/2013 3
SARAT
F ORTRAN RTRAN @ CIVI L I /I I
3. Write a program to input N number of ages and find the maximum, minimum and average age and to count the number of infants, children, teenagers and adults based on the following criteria: bel below 5........I nfants 5 – 12 ................childr ................childre en 13 – 19 19... ...... ......T ...T eenagers 20 and above...... ......A A dults dults c
1 2
3
4
5
All
Ages di mensi ensi on a( 100 00)) i nt eger b, c , d, e wr i t e( *, *) ' Ent er t he number of dat as' r ead( * , * ) n do 2 i =1, n wr i t e( * , 1) i f or mat ( 1x, ' Ent er ' , i 3, ' t h dat a' ) r ea e ad( * , * ) a( a( i ) co cont i nue max= ax =a( 1) do 3 i =1, n i f ( a( i ) . gt . max) t hen max=a( i ) el s e endi f co cont i nue wr i t e( *, *) ' The maxi mum age i s' wr i t e( *, *) max mi n=a( 1) do 4 i =1, n i f ( mi n. gt . a( i ) ) t hen mi n=a( i ) el s e endi f co cont i nue wr i t e( *, *) ' The mi ni mum age i s' wr i t e( * , * ) mi n s um=0 do 5 i =1, n s um=s um+a( i ) co cont i nue avg= av g=s um/ n wr i t e( *, *) ' The aver age age i s' wr i t e( * , * ) avg e=0 b=0 c=0 c=0 d=0 do 6 i =1, n Page Page 3 Created on September 05,2006 00:00 00:00 am
4/15/201 4/15/2013 3
SARAT
F ORTRAN @CIVI L I /II
6
All
i f ( a( i ) . l t . 5) t hen e=e+1 el s e i f ( a( i ) . l t . 13) t hen b=b+1 el s e i f ( a( i ) . l t . 20) t hen c=c+1 el s e d=d+1 endi f endi f endi f cont i nue wr i t e( *, *) ' The number wr i t e( * , * ) e wr i t e( *, *) ' The number wr i t e( * , * ) b wr i t e( *, *) ' The number wr i t e( * , * ) c wr i t e( *, *) ' The number wr i t e( * , * ) d wr i t e( * , * ) stop end
of i nf ant s i s' of chi l dr en i s' of t eenager s i s' of adul t s i s'
Page 4 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
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 ams f unct i on k( n, i ) k=( n- n/ 10**i *10**i ) / 10**( i - 1) return end
c
Mai n Pr ogr am di mensi on a( 10) i nt eger sum, b, a, c, o, p wr i t e( *, *) ' Ent er any i nt eger number ' r ead( * , * ) l i =0 i f ( l / 10* *i . l t . 10) t hen i =i +1 el s e i =i +1 got o1 endi f
1
2
3
do 2 j =1, i a( j ) =k( l , j ) cont i nue sum=0 do 3 p=1, i b=a( p) c=i - p o=b*10* * c sum=sum+o cont i nue wr i t e( *, *) ' The r ever se of t he gi ven i nt eger i s' wr i t e( * , * ) s um stop end
All
Page 5 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
5. Write a function subprogram to evaluate the value of f(x) where 1 – x2 , if x<0.1 2 x f(x) = , if 0.1 x 0.9 2 x –1 , if 0.9
c
All
Funct i ons f unct i on y( x) i f ( x. l t . 0. 1) t hen y=1- x** 2 el s e i f ( x. l e. 0. 9) t hen y=x* * 2 el s e i f ( x. l t . 1) t hen y=x** 2- 1 el s e y=0 endi f endi f endi f return end Mai n Pr ogr am wr i t e( *, *) ' Some cal cul at i ons of t he f unct i on ar e f ol l ows' do 1 l =1, 20, 1 x=l / 10. z=y( x) wr i t e( * , 2) x, z 2 f or mat ( 1x, ' For ' , f 7. 2, ' f ( x) = ' , f 7. 2, / ) 1 cont i nue wr i t e( * , * ) ' For f ur t her c al c ul at i on Ent er val ue of x' r ead( * , * ) x z=y( x) wr i t e( * , 3) z 3 f or mat ( 1x, ' F( x) =' , f 5. 2) stop end
Page 6 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
6. Write a function to convert degrees to radian value and use it to calculate the radian equivalent of 5,10,15....................175,180 degrees [180 degrees =I I radian] c
c
2 1 3
5
4
All
Funct i ons f unc t i on r ( x) r =x/ 180 return end Mai n Pr ogr am do 1 i =5, 180, 5 y=i a=r ( y) wr i t e( * , 2) y, a f or mat ( 1x, f 8. 2, ' D =' , f 5. 2, ' R' ) cont i nue wr i t e( *, *) ' For f ur t her cal cul at i on ent er val ue i n degr ee' r ead( * , * ) y a=r ( y) wr i t e( * , 2) y, a wr i t e( *, *) ' To r edo ent er 1 & t o exi t ent er 2' r ead( * , * ) l i f ( l . eq. 1) got o3 i f ( l . eq. 2) got o4 got o5 st op end
Page 7 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
7. Write a function to convert centigrade to Fahrenheit scale and use it to calculate the Fahrenheit equivalent of 1,2,3,.............39,40 degrees centigrade. [F =(C * 1.8) + 32] c c
c
3 1 6
4
5
All
100 Fahr enhei t t o cent i gr ade Funct i ons f unct i on g( c) g=9* c/ 5. +32 return end Mai n Pr ogr am do 1 i =1, 40, 1 c=i f =g( c) wr i t e( * , 3) c, f f or mat ( 1x, f 7. 2, ' C =' , f 7. 2, ' F' ) cont i nue wr i t e( * , * ) ' For f ur t her c al c ul at i on c al c ul at i on ent er 1 & t o exi t *ent er 2' r ead( * , * ) n i f ( n. eq. 1) got o4 i f ( n. eq. 2) got o5 got o6 wr i t e( * , * ) ' Ent er val ue i n c el ci us ' r ead( * , * ) c f =g( c) wr i t e( * , 3) c, f got o6 st op end
Page 8 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
8. Write a program to calculate mean and standard deviation from N number of data. 2 Mean = x / n SD = sqrt( x / n – ( x / n ) ) c
All
Mean & St andar d Devi at i on i nt eger sum1, sum2 wr i t e( *, *) ' Ent er number s of t er ms' r ead( * , * ) n sum1=0 sum2=0 do 1 l =1, n, 1 wr i t e( * , 2) l 2 f or mat ( 1x, ' Ent er ' , i 3, ' t h t er m' ) r ead( * , * ) m sum1=sum1+m sum2=sum2+m** 2 1 cont i nue Mean=sum1/ n var =sum2 sd=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) stop end
Page 9 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
9. Write a program to find out whether a given square matrix is symmetric [aij =a ji for all elements] c
4 1
3 2
All
Symmet r i c Mat r i x Di mensi on a( 10, 10) wr i t e( *, *) ' Ent er t he or der of squar e mat r i x' r ead( * , * ) k wr i t e( *, *) ' Ent er t he t er ms of t he mat r i x' do 1 i =1, k, 1 wr i t e( * , 4) i f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' ) r ead( * , * ) ( a( i , j ) , j =1, k) cont i nue n=0 do 2 i =1, k, 1 do 3 j =1, k, 1 i f ( a( i , j ) . eq. a( j , i ) ) t hen el s e n=n+1 endi f cont i nue cont i nue i f ( n. eq. 0) t hen wr i t e( *, *) ' The gi ven mat r i x i s symmet r i c' el s e wr i t e( *, *) ' The gi ven mat r i x i s not symmet r i c' endi f stop end
Page10 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
10. Write a program to find out whether a given square matrix is skew symmetric or not. [aij =-a ji for all non-diagonal elements and all diagonal elements must be zero] c
4 1
3 2
All
Skew- Symmet r i c Mat r i x Di mensi on a( 10, 10) wr i t e( *, *) ' Ent er t he or der of squar e mat r i x' r ead( * , * ) k wr i t e( *, *) ' Ent er t he t er ms of t he mat r i x' do 1 i =1, k, 1 wr i t e( * , 4) i f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' ) r ead( * , * ) ( a( i , j ) , j =1, k) cont i nue n=0 do 2 i =1, k, 1 do 3 j =1, k, 1 i f ( i . eq. j ) t hen i f ( a( i , j ) . eq. 0) got o3 n=n+1 got o3 endi f i f ( a( i , j ) . eq. - a( j , i ) ) t hen el s e n=n+1 endi f cont i nue cont i nue i f ( n. eq. 0) t hen wr i t e( *, *) ' The gi ven mat r i x i s skew symmet r i c. ' el s e wr i t e( *, *) ' The gi ven mat r i x i s not skew symmet r i c. ' endi f stop end
Page11 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
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 her di 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 1s t 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' r ead( * , * ) k , l i f ( m. eq. l ) t hen i f ( n. eq. k) t hen got o11 el s e endi f el s e endi f wr i t e( *, *) ' The mat r i x addi t i on i s not def i ned' stop 11 2 1
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, 1 wr i t e( * , 2) i f or mat ( 1x, ' Ent er t er ms i n' , i 2, ' t h r ow' ) r ead( * , * ) ( a( i , j ) , j =1, n) 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, k wr 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, l do 14 j =1, k d( i , j ) =c( j , i ) 14 cont i nue 13 cont i nue do 5 i =1, m, 1 do 5 j =1, n, 1 All
Page12 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
5
e( i , j ) =a( i , j ) +d( i , j ) cont i nue
6
wr i t e( * , * ) ' The f i r s t mat r i x i s f ol l ows ' do 6 i =1, m wr i t e( * , * ) ( a( i , j ) , j =1, n) cont i nue
8
wr i t e( *, *) ' The second mat r i x i s f ol l ows' do 8 i =1, k wr i t e( * , * ) ( c( i , j ) , j =1, l ) 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, l wr i t e( * , * ) ( d( i , j ) , j =1, k) 15 cont i nue wr i t e( * , * ) ' The s um mat r i x i s f ol l ows ' do 10 i =1, m wr i t e( * , * ) ( 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 or significant up to 6 decimal places: sinx =x – x3/3! +x5/5! – x7/7! +.............. c
All
Sum of Ser i es 5 wr i t e( *, *) ' To ent er number of t er ms ent er 1 or 2 f or s um upt o si x *deci mal pl aces' r ead( * , * ) n i f ( n. eq. 1) t hen wr i t e( *, *) ' Ent er number of t er ms' r ead( * , * ) l el s e i f ( n. eq. 2) t hen got o4 el s e got o5 endi f Page13 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
4
2
1 3
endi f wr i t e( *, *) ' Ent er val ue of x i n degr ees' r ead( * , * ) x x=( x*22/ 7. ) / 180 sum=x t er m=x m=1 i =0 i =i +2 ai =i t er m=- t er m*x*x/ ( ai *( ai +1) ) i f ( n. eq. 1) t hen m=m+1 i f ( m. gt . l ) t hen got o1 el s e sum=sum+t er m got o2 endi f el s e i f ( abs( t er m) . l t . 0. 000001) t hen got o1 el s e sum=sum+t er m got o2 endi f endi f wr i t e( *, 3) sum f or mat ( 3x, ' Si nx=' , f 15. 10) stop end
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
All
Sum of Ser i es f or cosx 5 wr i t e( *, *) ' To ent er number of t er ms ent er 1 or 2 f or s um upt o si x *deci mal pl aces' r ead( * , * ) n i f ( n. eq. 1) t hen wr i t e( *, *) ' Ent er number of t er ms' r ead( * , * ) l Page14 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
4
2
1 3
el s e i f ( n. eq. 2) t hen got o4 el s e got o5 endi f endi f wr i t e( *, *) ' Ent er val ue of x i n degr ees' r ead( * , * ) x x=( x*22/ 7. ) / 180 sum=1 t er m=1 m=1 i =- 1 i =i +2 ai =i t er m=- t er m*x*x/ ( ai *( ai +1) ) i f ( n. eq. 1) t hen m=m+1 i f ( m. gt . l ) t hen got o1 el s e sum=sum+t er m got o2 endi f el s e i f ( abs( t er m) . l t . 0. 000001) t hen got o1 el s e sum=sum+t er m got o2 endi f endi f wr i t e( *, 3) sum f or mat ( 3x, ' Cosx=' , f 15. 10) stop end
14. Write a program to compute the sum of the following series up to n terms or significant up to 6 decimal places: ex =1 +x/1! +x2/2! +x3/3 +.............. c 5
All
Sum of ser i es f or exponent i al wr i t e( *, *) ' To ent er number of t er ms ent er 1 or 2 f or s um upt o si x Page15 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
4
2
1 3
*deci mal pl aces' r ead( * , * ) n i f ( n. eq. 1) t hen wr i t e( *, *) ' Ent er number of t er ms' r ead( * , * ) l el s e i f ( n. eq. 2) t hen got o4 el s e got o5 endi f endi f wr i t e( * , * ) ' Ent er val ue of x' r ead( * , * ) x sum=1 t er m=1 m=1 i =0 i =i +1 ai =i t er m=t er m* x/ ai i f ( n. eq. 1) t hen m=m+1 i f ( m. gt . l ) t hen got o1 el s e sum=sum+t er m got o2 endi f el s e i f ( t er m. l t . 0. 000001) t hen got o1 el s e sum=sum+t er m got o2 endi f endi f wr i t e( *, 3) sum f or mat ( 3x, ' e**x=' , f 25. 10) stop end
15. Write a program to compute the sum of the following series up to n terms or significant up to 6 decimal places: e-x =1 – x/1! +x2/! – x3/3! +............ All
Page16 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
c 5
4
2
1 3
All
Sum of ser i es f or negat i ve power ed exponent i al wr i t e( *, *) ' To ent er number of t er ms ent er 1 or 2 f or s um upt o si x *deci mal pl aces' r ead( * , * ) n i f ( n. eq. 1) t hen wr i t e( *, *) ' Ent er number of t er ms' r ead( * , * ) l el s e i f ( n. eq. 2) t hen got o4 el s e got o5 endi f endi f wr i t e( * , * ) ' Ent er val ue of x' r ead( * , * ) x x=abs( x) sum=1 t er m=1 m=1 i =0 i =i +1 ai =i t er m=- t er m* x/ ai i f ( n. eq. 1) t hen m=m+1 i f ( m. gt . l ) t hen got o1 el s e sum=sum+t er m got o2 endi f el s e i f ( abs( t er m) . l t . 0. 000001) t hen got o1 el s e sum=sum+t er m got o2 endi f endi f wr i t e( *, 3) sum f or mat ( 3x, ' e**x=' , f 25. 10) stop end Page17 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
16. Write a program to compute the sum of the following series up to n terms or significant up to 6 decimal places: log(1 +x) =1 – x +x2/2 – x3/3 +x4/4 - ................ c
All
Sum of s er i es f or l og 5 wr i t e( *, *) ' To ent er number of t er ms ent er 1 or 2 f or s um upt o si x *deci mal pl aces' r ead( * , * ) n i f ( n. eq. 1) t hen wr i t e( *, *) ' Ent er number of t er ms' r ead( * , * ) l el s e i f ( n. eq. 2) t hen got o4 el s e got o5 endi f endi f 4 wr i t e( * , * ) ' Ent er val ue of x' r ead( * , * ) x x=abs( x) sum=1- x t er m=- x m=2 i =1 2 i =i +1 ai =i t er m=- t er m*x*( ai - 1) / ai i f ( n. eq. 1) t hen m=m+1 i f ( m. gt . l ) t hen got o1 el s e sum=sum+t er m got o2 endi f el s e i f ( abs( t er m) . l t . 0. 000001) t hen got o1 el s e sum=sum+t er m got o2 endi f endi f Page18 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
1 3
wr i t e( *, 3) sum f or mat ( 3x, ' l og( 1+x) =' , f 25. 10) stop end
17. Write a function subprogram to calculate the factorial of a number and use it to calculate the value of nCr nC r =n!/r!(n – r)! c c
1 c
2
nCr Funct i ons f unc t i on f ( k) f =1 do 1 i =1, k f =f *i cont i nue return end Mai n Pr ogr am wr i t e( *, *) ' Ent er val ues of n & r f or nCr ' r ead( * , * ) n, l m=f ( n) / ( f ( l ) * f ( n- l ) ) wr i t e( * , 2) n, l , m f or mat ( 1x, ' C( ' , i 2, ' , ' , i 2, ' ) = ' , i 3) stop end
18. Write a program to multiply two matrices of order M * N and N * L c
Mat r i x Mul t i pl i c at 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 1s t mat r i x i n t he f or m r , c ' r ead( * , * ) k , l wr 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 hen wr i t e( * , * ) ' The mat r i x mul t i pl i c at i on i s not def i ned' got o99 endi f wr i t e( *, *) ' Ent er t he t er ms of t he mat r i x' do 1 i =1, k
All
Page19 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
wr i 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, m wr i t e( * , 4) i 4 f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' ) r ead( * , * ) ( b( i , j ) , j =1, n) 3 cont i nue do 5 do 6 c( i , j do 7 c( i , j 7 cont i 6 cont i 5 cont i
i =1, k j =1, n ) =0 o=1, l ) =c( i , j ) +a( i , o) * b( o, j ) nue nue nue
wr i t e( *, *) ' The pr oduct mat r i x i s' do 8 i =1, k wr i t e( * , 9) ( c( i , j ) , j =1, n) 9 f or mat ( 1x, 100f 5. 2) 8 cont i nue 99 st op end
All
Page20 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
19. Write a program to compute the determinant of a 3 * 3 matrix. c
Mat r i x Mul t i pl i c at 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, 3 wr i 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, 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 stop end 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 is divisible by 100 it must also be divisible by 400. example : 1996 and 1600 are leap years but 1900 is not.]
c
1
All
Leap year s wr i t e( *, *) ' Ent er t he r ange of year s f or t he l eap year ' r ead( *, *) m, n wr i t e( *, *) ' The l eap year s wi t hi n t he gi ven r ange ar e : ' do 1 i =m, n i f ( mod( i , 4) . eq. 0) t hen i f ( mod( i , 100) . eq. 0) t hen i f ( mod( i , 400) . eq. 0) t hen wr i t e( * , * ) i endi f el s e wr i t e( * , * ) i endi f endi f cont i nue stop end Page21 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
21. Write a program to arrange an array of arbitrary data into ascending order. c
Asc endi ng or der i nt eger c di mensi on a( 100) , b( 100) wr i t e( *, *) ' Ent er number of t er ms' r ead( * , * ) n do 1 i =1, n, 1 wr i t e( * , 2) i 2 f or mat ( 1x, ' Ent er ' , i 3, ' t h t er m' ) r ead( * , * ) a( i ) 1 cont i nue do 3 i =1, n- 1 do 3 j =i +1, n i f ( a( i ) . gt . a( j ) ) t hen t =a( i ) a( i ) =a( j ) a( j ) =t endi f 3 cont i nue wr 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) stop end
All
Page22 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
22. Write a program to input a matrix of given dimension and compute the sum of any given row. c
Sum Di 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 ' r ead( * , * ) k , l wr i t e( *, *) ' Ent er t he t er ms of t he mat r i x' do 1 i =1, k, 1 wr 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( * , * ) i sum=0 do 3 j =1, l , 1 sum=sum+a( i , j ) 3 cont i nue wr i t e( *, *) ' The sum of el ement s of gi ven r ow i s : ' wr i t e( * , 5) s um 5 f or mat ( 1x, f 5. 2) stop end
All
Page23 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
23. Write a program to input a matrix of given dimension and compute the sum of any given column. c
Sum Di 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 ' r ead( * , * ) k , l wr i t e( *, *) ' Ent er t he t er ms of t he mat r i x' do 1 i =1, k, 1 wr 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( * , * ) j sum=0 do 3 i =1, l , 1 sum=sum+a( i , j ) 3 cont i nue wr i t e( *, *) ' The sum of el ement s of gi ven col umn i s : ' wr i t e( * , 5) s um 5 f or mat ( 1x, f 5. 2) stop end
All
Page24 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
24. Write a program to input a matrix of given dimension and find the maximum value along with its position.(row and column) c
Sum Di mensi on a( 10, 10) r eal max1, max2 wr i t e( * , * ) ' Ent er r ead( * , * ) k , l wr i t e( *, *) ' Ent er do 1 i =1, k, 1 wr i t e( * , 4) i 4 f or mat ( 1x, ' Ent er r ead( * , * ) ( a( i , j ) , 1 cont i nue
t he or der of mat r i x i n t he f or m r , c ' t he t er ms of t he mat r i x' t er ms of ' , i 2, ' t h r ow' ) j =1, l )
wr i t e( *, *) ' Ent er t he or der of el ement t o f i nd mi n. and max. number * al ong wi t h i t s pos i t i on' r ead( *, *) m, n max1=a( m, n) do 3 j =1, l i 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, k i f ( max2. l t . a( j , n) ) t hen max2=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 stop end
All
Page25 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
25. Write a program to input an array of N elements and find the minimum and maximum values along with their position. c
Mi nmax Di mensi on a( 100) r eal max1, mi n1 wr i t e( *, *) ' Ent er number of t er ms' r ead( * , * ) k wr i t e( *, *) ' Ent er t he t er ms of t he mat r i x' do 1 i =1, k, 1 wr i t e( * , 4) i 4 f or mat ( 1x, ' Ent er ' , i 2, ' t h t er m' ) r ead( * , * ) a( i ) 1 cont i nue max1=a( 1) do 3 j =1, k i f ( max1. l t . a( j ) ) t hen max1=a( j ) endi f 3 cont i nue mi n1=a( 1) do 6 j =1, k i f ( mi n1. ge. a( j ) ) t hen mi 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 stop end
All
Page26 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
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 n Di mensi on a( 10, 10) r eal mi n1, mi n2 wr i t e( * , * ) ' Ent er r ead( * , * ) k , l wr i t e( *, *) ' Ent er do 1 i =1, k, 1 wr i t e( * , 4) i 4 f or mat ( 1x, ' Ent er r ead( * , * ) ( a( i , j ) , 1 cont i nue
t he or der of mat r i x i n t he f or m r , c ' t he t er ms of t he mat r i x' t er ms of ' , i 2, ' t h r ow' ) j =1, l )
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 pos i t i on' r ead( *, *) m, n mi n1=a( m, n) do 5 j =1, l , 1 i f ( mi n1. ge. a( m, j ) ) t hen mi n1=a( m, j ) endi f 5 cont i nue mi n2=a( m, n) do 7 j =1, k, 1 i f ( mi n2. ge. a( j , n) ) t hen mi 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 stop end
All
Page27 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
27. Write a program to input square matrix and find the sum of all the non-diagonal elements. c
4 1
3 2
All
Sum Di mensi on a( 10, 10) wr i t e( *, *) ' Ent er t he or der of squar e mat r i x' r ead( * , * ) k wr i t e( *, *) ' Ent er t he t er ms of t he mat r i x' do 1 i =1, k, 1 wr i t e( * , 4) i f or mat ( 1x, ' Ent er t er ms of ' , i 2, ' t h r ow' ) r ead( * , * ) ( a( i , j ) , j =1, k) cont i nue sum=0 do 2 i =1, k, 1 do 3 j =1, k, 1 i f ( i . eq. j ) t hen el s e sum=sum+a( i , j ) endi f cont i nue 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( * , * ) s um stop end
Page28 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
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 7 i nt eger a di 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' r ead( * , * ) ( a( i ) , i =1, n)
1 2
All
sum=0 do 1 i =1, n i f ( mod( a( i ) , 5) . eq. 0) t hen i f ( mod( a( i ) , 7) . ne. 0) t hen wr i t e( * , * ) a( i ) sum=sum+a( i ) el s e endi f el s e endi f cont i nue wr i t e( * , * ) ' The s um of t he r es ul t i ng t er ms i s : ' wr i t e( * , 2) s um f or mat ( 1x, f 5. 2) stop end
Page29 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
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 5 i nt eger a di 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' r ead( * , * ) ( a( i ) , i =1, n)
1 2
All
sum=0 do 1 i =1, n i f ( mod( a( i ) , 7) . eq. 0) t hen i f ( mod( a( i ) , 5) . ne. 0) t hen wr i t e( * , * ) a( i ) sum=sum+a( i ) el s e endi f el s e endi f cont i nue wr i t e( * , * ) ' The s um of t he r es ul t i ng t er ms i s : ' wr i t e( * , 2) s um f or mat ( 1x, f 5. 2) stop end
Page30 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
30. Write a subroutine to calculate the base area, total surface area, and volumeof the cylinder and use it for at least 3 sets of data in the main program and display the result. c
Sub r out i ne s ubr out i ne ar ea( r , l , b, c, t , v) b=22/ 7. *r *r c=2*22/ 7. *r *l t =b+c v=b* l return end
c
Mai n pr ogr am do 1 i =1, 3 wr i t e( * , 2) i f or mat ( 1x, ' Ent er ' , i 2, ' t h s et of ( r , l ) ' ) r ead( * , * ) r , l wr i t e( * , 3) i f or mat ( 1x, ' The r es ul t s f or ' , i 2, ' s et 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( * , * ) b wr i t e( *, *) ' The ar ea of t he cur ved sur f ace i s' wr i t e( * , * ) c wr i t e( * , * ) ' The t ot al ar ea i s ' wr i t e( * , * ) t wr i t e( *, *) ' The vol ume of t he cyl i nder i s' wr i t e( * , * ) v cont i nue stop end
2 3
1
All
Page31 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
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 Pythagoras theorem h2 =p2 +b2 ] c
1
2
Ri ght angl ed t r i angl e di mensi on a( 3) wr i t e( * , * ) ' Ent er t he s i des of t he t r i angl e' r ead( * , * ) ( a( j ) , j =1, 3) max=0 do 1 i =1, 3 i f ( a( i ) . gt . max) t hen max=a( i ) el s e endi f cont i nue s=0 do 2 i =1, 3 i f ( a( i ) . ne. max) t hen s=s+a( i ) ** 2 el s e endi f cont i nue r =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 s e wr i t e( * , * ) ' The t r i angl e i s not r i ght angl ed. ' endi f stop end
32. Write a program to generate the square roots of all the integer numbers within a given range. c
All
Squar e Root wr i t e( * , * ) ' Ent er r ange f or t he i nt er ger s ' r ead( * , * ) l , m do 1 i =l , m, 1 a=i s=sqr t ( a) wr i t e( * , 2) i , s 2 f or mat ( 1x, ' sqr t ( ' , i 3, ' ) =' , f 7. 2) 1 cont i nue stop end
Page32 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
33. Write a program to generate all the prime numbers within a given range. c
1 2
7
9
All
Pr i me Number s wr i t e( *, *) ' Ent er r ange f or t he pr i me number s' r ead( * , * ) l , m i =0 n=l k=1 k=k+1 i f ( n. eq. 1) t hen n=n+1 got o1 el s e endi f i f ( k. gt . n/ 2. ) t hen i =i +1 wr i t e( * , 7) i , n f or mat ( 2x, i 3, ' ) ' , 2x, i 10) i f ( n. gt . m) got o9 n=n+1 got o1 el s e i f ( n- n/ k* k. eq. 0) t hen n=n+1 i f ( n. gt . m) got o9 got o1 el s e got o2 endi f endi f st op end
Page33 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
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 interest rate for 1,2,3,........25 years. c c
c
Si mpl e I nt er est Funct i ons f unct i on f ( x, y, n) f =x*y* n/ 100 return end Mai n Pr ogr am wr i t e( *, *) ' Ent er Pr i nci pal Amount ' r ead( * , * ) a wr i t e( * , * ) ' Ent er I nt er es t Rat e' r ead( * , * ) b do 1 l =1, 25 d=f ( a, b, l ) wr i t e( * , 2) l 2 f or mat ( 1x, ' The t ot al i nt er es t f or ' , i 3, ' year s i s ' ) wr i t e( * , * ) d 1 cont i nue stop end
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
1
All
Ser i es i nt eger sum wr i t e( *, *) ' Ent er number of t er ms t o be added' r ead( * , * ) n k=- 1 l =- 1 sum=0 do 1 i =1, n k=k+2 l =l +3 sum=sum+( k) *( l ) cont i nue wr i t e( * , * ) ' The s um of t he s er i es i s ' wr i t e( * , * ) s um stop end
Page34 Created on September 05,2006 00:00 am
4/15/2013
SARAT
F ORTRAN @CIVI L I /II
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
1
Ser i es i nt eger sum wr i t e( *, *) ' Ent er number of t er ms t o be added' r ead( * , * ) n k=- 2 l =0 sum=0 do 1 i =1, n k=k+3 l =l +2 sum=sum+( k) *( l ) cont i nue wr i t e( * , * ) ' The s um of t he s er i es i s ' wr i t e( * , * ) s um stop end
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
1
All
Ser i es i nt eger sum wr i t e( *, *) ' Ent er number of t er ms t o be added' r ead( * , * ) n k=- 2 l =- 2 sum=0 do 1 i =1, n k=k+3 l =l +4 sum=sum+( k) *( l ) cont i nue wr i t e( * , * ) ' The s um of t he s er i es i s ' wr i t e( * , * ) s um stop end
Page35 Created on September 05,2006 00:00 am
4/15/2013