You are on page 1of 36

SARAT

FORTRAN @ CIVIL I/II

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 write(*,*)'For the roots of quadratic equation' write(*,*)'enter values of a,b,c' read(*,*)a,b,c if(b**2-4*a*c)1,2,3 write(*,*)'real value of x doesnot exist' goto7 x=-b/(2*a) write(*,*)'the value of x is' write(*,*)x goto7 x1=(-b+sqrt(b**2-4*a*c))/(2*a) x2=(-b-sqrt(b**2-4*a*c))/(2*a) write(*,*)'the values of x are' write(*,*)x1,x2 write(*,*)'Enter 1 to redo & 2 to exit' read(*,*)l if(l.eq.1)goto5 if(l.eq.2)goto6 goto7 stop end

1 2

All

Page 1 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

2. Write a program to compute the values of two unknowns x1 and x2 in a system of linear equations given the values of a1, b1, c1 and a2, b2, c2.The program should generate an error message if these equations of straight are parallel. The program should also ask whether or not re-compute for another equation and do accordingly a1x1 + b1x1 = c1 a2x1 + b2x2 = c2 c 2 Linear Equations write(*,*)'Enter the values of a1,b1,c1 for a1x1+b1x2=c1' read(*,*)a1,b1,c1 write(*,*)'Enter the values of a2,b2,c2 for a2x1+b2x2=c2' read(*,*)a2,b2,c2 a=a2/a1 b=b2/b1 c=c2/c1 if(a.eq.b)then if(b.eq.c)then goto5 else goto6 endif else goto6 endif write(*,*)'The lines are parallel to each other' goto4 d=a1*b2-a2*b1 x1=(b2*c1-c2*b1)/d x2=(a1*c2-a2*c1)/d write(*,*)'The solutions are follows:' write(*,1)x1,x2 format(1x,'x1=',f5.2,/,1x,'x2=',f5.2) write(*,*)'Enter 1 to redo & 2 to exit' read(*,*)l if(l.eq.1)goto2 if(l.eq.2)goto3 goto4 stop end

5 6

1 4

All

Page 2 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

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: below 5........Infants 5 12 ................children 13 19.........Teenagers 20 and above......Adults c Ages dimension a(100) integer b,c,d,e write(*,*)'Enter the number of datas' read(*,*)n do 2 i=1,n write(*,1)i format(1x,'Enter ',i3,'th data') read(*,*)a(i) continue max=a(1) do 3 i=1,n if(a(i).gt.max)then max=a(i) else endif continue write(*,*)'The maximum age is' write(*,*)max min=a(1) do 4 i=1,n if(min.gt.a(i))then min=a(i) else endif continue write(*,*)'The minimum age is' write(*,*)min sum=0 do 5 i=1,n sum=sum+a(i) continue avg=sum/n write(*,*)'The average age is' write(*,*)avg e=0 b=0 c=0 d=0 do 6 i=1,n
Page 3 Created on September 05,2006 00:00 am 4/15/2013

1 2

All

SARAT

FORTRAN @ CIVIL I/II

if(a(i).lt.5)then e=e+1 else if(a(i).lt.13)then b=b+1 else if(a(i).lt.20)then c=c+1 else d=d+1 endif endif endif continue write(*,*)'The number write(*,*)e write(*,*)'The number write(*,*)b write(*,*)'The number write(*,*)c write(*,*)'The number write(*,*)d write(*,*) stop end

of infants is' of children is' of teenagers is' of adults is'

All

Page 4 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

4. Write a program to reserve a given integer number using a subprogram. c c Reverse of an integer Sub Programs function k(n,i) k=(n-n/10**i*10**i)/10**(i-1) return end Main Program dimension a(10) integer sum,b,a,c,o,p write(*,*)'Enter any integer number' read(*,*)l i=0 if(l/10**i.lt.10)then i=i+1 else i=i+1 goto1 endif do 2 j=1,i a(j)=k(l,j) continue sum=0 do 3 p=1,i b=a(p) c=i-p o=b*10**c sum=sum+o continue write(*,*)'The reverse of the given integer is' write(*,*)sum stop end

All

Page 5 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

5. Write a function subprogram to evaluate the value of f(x) where 1 x2 , if x< 0.1 2 , if 0.1 x 0.9 f(x) = x x2 1 , if 0.9 < x < 1.0 0 , otherwise and use it to calculate the value of f(x) for x = 0.0,0.1,0.2,..............,1.9,2.0 c Functions function y(x) if(x.lt.0.1)then y=1-x**2 else if(x.le.0.9)then y=x**2 else if(x.lt.1)then y=x**2-1 else y=0 endif endif endif return end Main Program write(*,*)'Some calculations of the function are follows' do 1 l=1,20,1 x=l/10. z=y(x) write(*,2)x,z 2 format(1x,'For ',f7.2,' f(x) = ',f7.2,/) 1 continue write(*,*)'For further calculation Enter value of x' read(*,*)x z=y(x) write(*,3)z 3 format(1x,'F(x) =',f5.2) stop end

All

Page 6 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL 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 = II radian] c Functions function r(x) r=x/180 return end Main Program do 1 i=5,180,5 y=i a=r(y) write(*,2)y,a format(1x,f8.2,'D =',f5.2,'R') continue write(*,*)'For further calculation enter value in degree' read(*,*)y a=r(y) write(*,2)y,a write(*,*)'To redo enter 1 & to exit enter 2' read(*,*)l if(l.eq.1)goto3 if(l.eq.2)goto4 goto5 stop end

2 1 3

All

Page 7 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL 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 100 Fahrenheit to centigrade Functions function g(c) g=9*c/5.+32 return end Main Program do 1 i=1,40,1 c=i f=g(c) write(*,3)c,f format(1x,f7.2,'C =',f7.2,'F') continue write(*,*)'For further calculation calculation enter 1 & to exit *enter 2' read(*,*)n if(n.eq.1)goto4 if(n.eq.2)goto5 goto6 write(*,*)'Enter value in celcius' read(*,*)c f=g(c) write(*,3)c,f goto6 stop end

3 1 6

All

Page 8 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

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 & Standard Deviation integer sum1,sum2 write(*,*)'Enter numbers of terms' read(*,*)n sum1=0 sum2=0 do 1 l=1,n,1 write(*,2)l 2 format(1x,'Enter ',i3,'th term') read(*,*)m sum1=sum1+m sum2=sum2+m**2 1 continue Mean=sum1/n var=sum2 sd=sqrt(var/n-(sum1/n)**2) write(*,3)Mean,sd 3 format(1x,'Mean =',i5,/,1x,'Standard Deviation =',f7.2) stop end

All

Page 9 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

9. Write a program to find out whether a given square matrix is symmetric [aij = aji for all elements] c Symmetric Matrix Dimension a(10,10) write(*,*)'Enter the order of square matrix' read(*,*)k write(*,*)'Enter the terms of the matrix' do 1 i=1,k,1 write(*,4)i format(1x,'Enter terms of ',i2,'th row') read(*,*)(a(i,j),j=1,k) continue n=0 do 2 i=1,k,1 do 3 j=1,k,1 if(a(i,j).eq.a(j,i))then else n=n+1 endif continue continue if(n.eq.0)then write(*,*)'The given matrix is symmetric' else write(*,*)'The given matrix is not symmetric' endif stop end

4 1

3 2

All

Page 10 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

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-Symmetric Matrix Dimension a(10,10) write(*,*)'Enter the order of square matrix' read(*,*)k write(*,*)'Enter the terms of the matrix' do 1 i=1,k,1 write(*,4)i format(1x,'Enter terms of ',i2,'th row') read(*,*)(a(i,j),j=1,k) continue n=0 do 2 i=1,k,1 do 3 j=1,k,1 if(i.eq.j)then if(a(i,j).eq.0)goto3 n=n+1 goto3 endif if(a(i,j).eq.-a(j,i))then else n=n+1 endif continue continue if(n.eq.0)then write(*,*)'The given matrix is skew symmetric.' else write(*,*)'The given matrix is not skew symmetric.' endif stop end

4 1

3 2

All

Page 11 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

11. Write a program to input two square matrices and add the first matrix with the transpose of the second matrix. c Addition with transpose of other dimension a(10,10),c(10,10),d(10,10),e(10,10) write(*,*)'For the addition of matrixes' write(*,*)'Enter the order for 1st matrix as r,c' read(*,*)m,n write(*,*)'Enter the order for 2nd matrix as r,c' read(*,*)k,l if(m.eq.l)then if(n.eq.k)then goto11 else endif else endif write(*,*)'The matrix addition is not defined' stop 11 write(*,*)'Enter the values of the terms of 1st matrix' do 1 i=1,m,1 write(*,2)i format(1x,'Enter terms in',i2,'th row') read(*,*)(a(i,j),j=1,n) continue

2 1

write(*,*)'Enter the values of the terms of 2nd matrix' do 12 i=1,k write(*,20)i 20 format(1x,,'Enter terms in ',i2,'th row') read(*,*)(c(i,j),j=1,l) 12 continue do 13 i=1,l do 14 j=1,k d(i,j)=c(j,i) 14 continue 13 continue do 5 i=1,m,1 do 5 j=1,n,1
All Page 12 Created on September 05,2006 00:00 am 4/15/2013

SARAT

FORTRAN @ CIVIL I/II

e(i,j)=a(i,j)+d(i,j) continue write(*,*)'The first matrix is follows' do 6 i=1,m write(*,*)(a(i,j),j=1,n) continue write(*,*)'The second matrix is follows' do 8 i=1,k write(*,*)(c(i,j),j=1,l) continue

write(*,*)'The transpose of second matrix is follows' do 15 i=1,l write(*,*)(d(i,j),j=1,k) 15 continue write(*,*)'The sum matrix is follows' do 10 i=1,m write(*,*)(e(i,j),j=1,n) 10 continue 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 5 Sum of Series write(*,*)'To enter number of terms enter 1 or 2 for sum upto six *decimal places' read(*,*)n if(n.eq.1)then write(*,*)'Enter number of terms' read(*,*)l else if(n.eq.2)then goto4 else goto5 endif
Page 13 Created on September 05,2006 00:00 am 4/15/2013

All

SARAT

FORTRAN @ CIVIL I/II

1 3

endif write(*,*)'Enter value of x in degrees' read(*,*)x x=(x*22/7.)/180 sum=x term=x m=1 i=0 i=i+2 ai=i term=-term*x*x/(ai*(ai+1)) if(n.eq.1)then m=m+1 if(m.gt.l)then goto1 else sum=sum+term goto2 endif else if(abs(term).lt.0.000001)then goto1 else sum=sum+term goto2 endif endif write(*,3)sum format(3x,'Sinx=',f15.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 5 Sum of Series for cosx write(*,*)'To enter number of terms enter 1 or 2 for sum upto six *decimal places' read(*,*)n if(n.eq.1)then write(*,*)'Enter number of terms' read(*,*)l
Page 14 Created on September 05,2006 00:00 am 4/15/2013

All

SARAT

FORTRAN @ CIVIL I/II

1 3

else if(n.eq.2)then goto4 else goto5 endif endif write(*,*)'Enter value of x in degrees' read(*,*)x x=(x*22/7.)/180 sum=1 term=1 m=1 i=-1 i=i+2 ai=i term=-term*x*x/(ai*(ai+1)) if(n.eq.1)then m=m+1 if(m.gt.l)then goto1 else sum=sum+term goto2 endif else if(abs(term).lt.0.000001)then goto1 else sum=sum+term goto2 endif endif write(*,3)sum format(3x,'Cosx=',f15.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 Sum of series for exponential write(*,*)'To enter number of terms enter 1 or 2 for sum upto six
Page 15 Created on September 05,2006 00:00 am 4/15/2013

All

SARAT

FORTRAN @ CIVIL I/II

1 3

*decimal places' read(*,*)n if(n.eq.1)then write(*,*)'Enter number of terms' read(*,*)l else if(n.eq.2)then goto4 else goto5 endif endif write(*,*)'Enter value of x' read(*,*)x sum=1 term=1 m=1 i=0 i=i+1 ai=i term=term*x/ai if(n.eq.1)then m=m+1 if(m.gt.l)then goto1 else sum=sum+term goto2 endif else if(term.lt.0.000001)then goto1 else sum=sum+term goto2 endif endif write(*,3)sum format(3x,'e**x=',f25.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 Page 16 Created on September 05,2006 00:00 am 4/15/2013

SARAT

FORTRAN @ CIVIL I/II

c 5

1 3

Sum of series for negative powered exponential write(*,*)'To enter number of terms enter 1 or 2 for sum upto six *decimal places' read(*,*)n if(n.eq.1)then write(*,*)'Enter number of terms' read(*,*)l else if(n.eq.2)then goto4 else goto5 endif endif write(*,*)'Enter value of x' read(*,*)x x=abs(x) sum=1 term=1 m=1 i=0 i=i+1 ai=i term=-term*x/ai if(n.eq.1)then m=m+1 if(m.gt.l)then goto1 else sum=sum+term goto2 endif else if(abs(term).lt.0.000001)then goto1 else sum=sum+term goto2 endif endif write(*,3)sum format(3x,'e**x=',f25.10) stop end
Page 17 Created on September 05,2006 00:00 am 4/15/2013

All

SARAT

FORTRAN @ CIVIL 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 Sum of series for log write(*,*)'To enter number of terms enter 1 or 2 for sum upto six *decimal places' read(*,*)n if(n.eq.1)then write(*,*)'Enter number of terms' read(*,*)l else if(n.eq.2)then goto4 else goto5 endif endif 4 write(*,*)'Enter value of x' read(*,*)x x=abs(x) sum=1-x term=-x m=2 i=1 2 i=i+1 ai=i term=-term*x*(ai-1)/ai if(n.eq.1)then m=m+1 if(m.gt.l)then goto1 else sum=sum+term goto2 endif else if(abs(term).lt.0.000001)then goto1 else sum=sum+term goto2 endif endif 5
Page 18 Created on September 05,2006 00:00 am 4/15/2013

All

SARAT

FORTRAN @ CIVIL I/II

1 3

write(*,3)sum format(3x,'log(1+x) =',f25.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 nCr Functions function f(k) f=1 do 1 i=1,k f=f*i continue return end Main Program write(*,*)'Enter values of n & r for nCr' read(*,*)n,l m=f(n)/(f(l)*f(n-l)) write(*,2)n,l,m format(1x,'C(',i2,',',i2,') = ',i3) stop end

18. Write a program to multiply two matrices of order M * N and N * L c Matrix Multiplication Dimension a(10,10),b(10,10),c(10,10) integer o write(*,*)'Enter the order of 1st matrix in the form r,c' read(*,*)k,l write(*,*)'Enter the order of matrix in the form r,c' read(*,*)m,n if(l.ne.m)then write(*,*)'The matrix multiplication is not defined' goto99 endif write(*,*)'Enter the terms of the matrix' do 1 i=1,k
All Page 19 Created on September 05,2006 00:00 am 4/15/2013

SARAT

FORTRAN @ CIVIL I/II

write(*,2)i 2 format(1x,'Enter terms of ',i2,'th row') read(*,*)(a(i,j),j=1,l) 1 continue

write(*,*)'Enter the terms of 2nd the matrix' do 3 i=1,m write(*,4)i 4 format(1x,'Enter terms of ',i2,'th row') read(*,*)(b(i,j),j=1,n) 3 continue do 5 i=1,k do 6 j=1,n c(i,j)=0 do 7 o=1,l c(i,j)=c(i,j)+a(i,o)*b(o,j) 7 continue 6 continue 5 continue write(*,*)'The product matrix is' do 8 i=1,k write(*,9)(c(i,j),j=1,n) 9 format(1x,100f5.2) 8 continue 99 stop end

All

Page 20 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

19. Write a program to compute the determinant of a 3 * 3 matrix. c Matrix Multiplication Dimension a(3,3) write(*,*)'Enter the terms of the 3 X 3 matrix' do 1 i=1,3 write(*,2)i 2 format(1x,'Enter terms of ',i2,'th row') read(*,*)(a(i,j),j=1,3) 1 continue 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)) write(*,*)'The determinant of the matrix is' write(*,3)d 3 format(1x,f9.2) continue 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 Leap years write(*,*)'Enter the range of years for the leap year' read(*,*)m,n write(*,*)'The leap years within the given range are :' do 1 i=m,n if(mod(i,4).eq.0)then if(mod(i,100).eq.0)then if(mod(i,400).eq.0)then write(*,*)i endif else write(*,*)i endif endif continue stop end
Page 21 Created on September 05,2006 00:00 am 4/15/2013

All

SARAT

FORTRAN @ CIVIL I/II

21. Write a program to arrange an array of arbitrary data into ascending order. c Ascending order integer c dimension a(100),b(100) write(*,*)'Enter number of terms' read(*,*)n do 1 i=1,n,1 write(*,2)i 2 format(1x,'Enter ',i3,'th term') read(*,*)a(i) 1 continue do 3 i=1,n-1 do 3 j=i+1,n if(a(i).gt.a(j))then t=a(i) a(i)=a(j) a(j)=t endif 3 continue write(*,*)'The terms in ascending order is :' write(*,4)(a(i),i=1,n) 4 format(1x,100f7.2) stop end

All

Page 22 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

22. Write a program to input a matrix of given dimension and compute the sum of any given row. c Sum Dimension a(10,10),b(100) write(*,*)'Enter the order of matrix in the form r,c' read(*,*)k,l write(*,*)'Enter the terms of the matrix' do 1 i=1,k,1 write(*,4)i 4 format(1x,'Enter terms of ',i2,'th row') read(*,*)(a(i,j),j=1,l) 1 continue write(*,*)'Enter the row to calculate the sum' read(*,*)i sum=0 do 3 j=1,l,1 sum=sum+a(i,j) 3 continue write(*,*)'The sum of elements of given row is :' write(*,5)sum 5 format(1x,f5.2) stop end

All

Page 23 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

23. Write a program to input a matrix of given dimension and compute the sum of any given column. c Sum Dimension a(10,10),b(100) write(*,*)'Enter the order of matrix in the form r,c' read(*,*)k,l write(*,*)'Enter the terms of the matrix' do 1 i=1,k,1 write(*,4)i 4 format(1x,'Enter terms of ',i2,'th row') read(*,*)(a(i,j),j=1,l) 1 continue write(*,*)'Enter the column to calculate the sum' read(*,*)j sum=0 do 3 i=1,l,1 sum=sum+a(i,j) 3 continue write(*,*)'The sum of elements of given column is :' write(*,5)sum 5 format(1x,f5.2) stop end

All

Page 24 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL 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 Dimension a(10,10) real max1,max2 write(*,*)'Enter the order of matrix in the form r,c' read(*,*)k,l write(*,*)'Enter the terms of the matrix' do 1 i=1,k,1 write(*,4)i 4 format(1x,'Enter terms of ',i2,'th row') read(*,*)(a(i,j),j=1,l) 1 continue write(*,*)'Enter the order of element to find min. and max. number * along with its position' read(*,*)m,n max1=a(m,n) do 3 j=1,l if(max1.lt.a(m,j))then max1=a(m,j) endif 3 continue max2=a(m,n) do 6 j=1,k if(max2.lt.a(j,n))then max2=a(j,n) endif 6 continue write(*,*)'The max. value along its row is :' write(*,8)max1 8 format(1x,f5.2) write(*,*)'The max. value along its column is :' write(*,8)max2 stop end

All

Page 25 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL 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 Minmax Dimension a(100) real max1,min1 write(*,*)'Enter number of terms' read(*,*)k write(*,*)'Enter the terms of the matrix' do 1 i=1,k,1 write(*,4)i 4 format(1x,'Enter',i2,'th term') read(*,*)a(i) 1 continue max1=a(1) do 3 j=1,k if(max1.lt.a(j))then max1=a(j) endif 3 continue min1=a(1) do 6 j=1,k if(min1.ge.a(j))then min1=a(j) endif 6 continue write(*,*)'The max. value is :' write(*,8)max1 8 format(1x,f5.2) write(*,*)'The min value is :' write(*,8)min1 stop end

All

Page 26 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL 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 Min Dimension a(10,10) real min1,min2 write(*,*)'Enter the order of matrix in the form r,c' read(*,*)k,l write(*,*)'Enter the terms of the matrix' do 1 i=1,k,1 write(*,4)i 4 format(1x,'Enter terms of ',i2,'th row') read(*,*)(a(i,j),j=1,l) 1 continue write(*,*)'Enter the order of element to find min.number * along with its position' read(*,*)m,n min1=a(m,n) do 5 j=1,l,1 if(min1.ge.a(m,j))then min1=a(m,j) endif 5 continue min2=a(m,n) do 7 j=1,k,1 if(min2.ge.a(j,n))then min2=a(j,n) endif 7 continue 8 format(1x,f5.2) write(*,*)'The min. value along its row is :' write(*,8)min1 write(*,*)'The min. value along its column is :' write(*,8)min2 stop end

All

Page 27 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

27. Write a program to input square matrix and find the sum of all the non-diagonal elements. c Sum Dimension a(10,10) write(*,*)'Enter the order of square matrix' read(*,*)k write(*,*)'Enter the terms of the matrix' do 1 i=1,k,1 write(*,4)i format(1x,'Enter terms of ',i2,'th row') read(*,*)(a(i,j),j=1,k) continue sum=0 do 2 i=1,k,1 do 3 j=1,k,1 if(i.eq.j)then else sum=sum+a(i,j) endif continue continue write(*,*)'The sum of non-diagonal elements of matrix is' write(*,*)sum stop end

4 1

3 2

All

Page 28 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL 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 Divisible by 5 but not by 7 integer a dimension a(100) write(*,*)'Enter number of terms' read(*,*)n write(*,*)'Enter the terms' read(*,*)(a(i),i=1,n) sum=0 do 1 i=1,n if(mod(a(i),5).eq.0)then if(mod(a(i),7).ne.0)then write(*,*)a(i) sum=sum+a(i) else endif else endif continue write(*,*)'The sum of the resulting terms is :' write(*,2)sum format(1x,f5.2) stop end

All

Page 29 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL 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 Divisible by 7 but not by 5 integer a dimension a(100) write(*,*)'Enter number of terms' read(*,*)n write(*,*)'Enter the terms' read(*,*)(a(i),i=1,n) sum=0 do 1 i=1,n if(mod(a(i),7).eq.0)then if(mod(a(i),5).ne.0)then write(*,*)a(i) sum=sum+a(i) else endif else endif continue write(*,*)'The sum of the resulting terms is :' write(*,2)sum format(1x,f5.2) stop end

All

Page 30 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

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 the result. c Sub routine subroutine area(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 Main program do 1 i=1,3 write(*,2)i format(1x,'Enter',i2,'th set of (r,l)') read(*,*)r,l write(*,3)i format(1x,'The results for',i2,' set of data are :') call area(r,l,b,c,t,v) write(*,*)'The area of the base is' write(*,*)b write(*,*)'The area of the curved surface is' write(*,*)c write(*,*)'The total area is' write(*,*)t write(*,*)'The volume of the cylinder is' write(*,*)v continue stop end

All

Page 31 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL 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 Right angled triangle dimension a(3) write(*,*)'Enter the sides of the triangle' read(*,*)(a(j),j=1,3) max=0 do 1 i=1,3 if(a(i).gt.max)then max=a(i) else endif continue s=0 do 2 i=1,3 if(a(i).ne.max)then s=s+a(i)**2 else endif continue r=sqrt(s) if(max.eq.r)then write(*,*)'The triangle is right angled.' else write(*,*)'The triangle is not right angled.' endif stop end

32. Write a program to generate the square roots of all the integer numbers within a given range. c Square Root write(*,*)'Enter range for the intergers' read(*,*)l,m do 1 i=l,m,1 a=i s=sqrt(a) write(*,2)i,s 2 format(1x,'sqrt(',i3,' ) =',f7.2) 1 continue stop end

All

Page 32 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

33. Write a program to generate all the prime numbers within a given range. c Prime Numbers write(*,*)'Enter range for the prime numbers' read(*,*)l,m i=0 n=l k=1 k=k+1 if(n.eq.1)then n=n+1 goto1 else endif if(k.gt.n/2.)then i=i+1 write(*,7)i,n format(2x,i3,')',2x,i10) if(n.gt.m)goto9 n=n+1 goto1 else if(n-n/k*k.eq.0)then n=n+1 if(n.gt.m)goto9 goto1 else goto2 endif endif stop end

1 2

All

Page 33 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL 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 Simple Interest Functions function f(x,y,n) f=x*y*n/100 return end Main Program write(*,*)'Enter Principal Amount' read(*,*)a write(*,*)'Enter Interest Rate' read(*,*)b do 1 l=1,25 d=f(a,b,l) write(*,2)l 2 format(1x,'The total interest for',i3,' years is') write(*,*)d 1 continue 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 Series integer sum write(*,*)'Enter number of terms to be added' read(*,*)n k=-1 l=-1 sum=0 do 1 i=1,n k=k+2 l=l+3 sum=sum+(k)*(l) continue write(*,*)'The sum of the series is' write(*,*)sum stop end

All

Page 34 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL 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 Series integer sum write(*,*)'Enter number of terms to be added' read(*,*)n k=-2 l=0 sum=0 do 1 i=1,n k=k+3 l=l+2 sum=sum+(k)*(l) continue write(*,*)'The sum of the series is' write(*,*)sum 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 Series integer sum write(*,*)'Enter number of terms to be added' read(*,*)n k=-2 l=-2 sum=0 do 1 i=1,n k=k+3 l=l+4 sum=sum+(k)*(l) continue write(*,*)'The sum of the series is' write(*,*)sum stop end

All

Page 35 Created on September 05,2006 00:00 am

4/15/2013

SARAT

FORTRAN @ CIVIL I/II

38. Write a program to generate the multiplication table of a given number within a given range. c Multiplication table write(*,*)'Enter the number to multiply,number to be multiplied fr *om and number up to be multiplied with' read(*,*)n,l,m do 1 i=l,m,1 k=n*i write(*,2)n,i,k 2 format(1x,i5,3x,'X',i5,'=',i8) 1 continue stop end

All

Page 36 Created on September 05,2006 00:00 am

4/15/2013

You might also like