Fortran作业2

合集下载

fortran编程习题答案

fortran编程习题答案

fortran编程习题答案Fortran编程习题答案Fortran是一种古老而强大的编程语言,广泛应用于科学计算和工程领域。

在学习Fortran编程的过程中,解决习题是一种非常有效的方法。

本文将为您提供一些Fortran编程习题的答案,帮助您更好地理解和掌握这门语言。

1. 习题一:编写一个Fortran程序,计算并输出1到100之间所有整数的平方。

程序代码如下:```fortranprogram squareimplicit noneinteger :: ido i = 1, 100print *, i, i**2end doend program square```2. 习题二:编写一个Fortran程序,计算并输出斐波那契数列的前20个数。

程序代码如下:```fortranprogram fibonacciimplicit noneinteger :: i, n, fib(20)fib(1) = 0fib(2) = 1do i = 3, 20fib(i) = fib(i-1) + fib(i-2)end dodo i = 1, 20print *, fib(i)end doend program fibonacci```3. 习题三:编写一个Fortran程序,计算并输出一个给定整数的阶乘。

程序代码如下:```fortranprogram factorialimplicit noneinteger :: i, n, resultprint *, "请输入一个整数:"read *, nresult = 1do i = 1, nresult = result * iend doprint *, n, "的阶乘为:", resultend program factorial```4. 习题四:编写一个Fortran程序,计算并输出一个给定整数是否为素数。

程序代码如下:```fortranprogram primeimplicit noneinteger :: i, n, countprint *, "请输入一个整数:"read *, ncount = 0do i = 2, n-1if (mod(n, i) == 0) thencount = count + 1end ifend doif (count == 0) thenprint *, n, "是素数"elseprint *, n, "不是素数"end ifend program prime```5. 习题五:编写一个Fortran程序,计算并输出一个给定整数的所有因子。

fortran作业

fortran作业

fortran作业第三章二、将数学式写成fortran表达式(1)a**2+4*b**3)/(a-b)(2)(-b+sqrt(b*b-4*a*c))/(2*a)(3)(6*sin((x+y)**2))/(2*a)(4)sin(y/(aqrt(x*x+y*y)))(5)sin(atan(aqrt(x*x+y*y))/(abs(c)))九、电路编程implicitreal(a-z)r1=30r2=60r3=45u=120i=(u/r1+u/r2+u/r3)print*,iend结果:8.666667十、六边形面积编程!mainprogramimplicitreal(a-z)i1=10i2=20i3=16i4=13i5=21i6=14i7=30i8=36i9=28area=x(i2,i3,i7)+x(i1,i7,i8)+x(i4,i8,i9)+x(i5,i6,i9)print*,'area=',areaend!functionprogramfunctionx(a1,a2,a3)b=(a1+a2+a3)/2x=sqrt(b*(b-a1)*(b-a2)*(b-a3))end结果:area=581.2570十一、分期付款编程read(*,*)d,p,rm=(log(p)-log(p-d*r))/log(1+r)m=m+1print*,mend十二、筹钱贷款编程!daikuanrealiread(*,*)a,r,ni=r+1d=(a*(i**n-1))/((i-1)*i**n)d=int(d*10+0.5)/10print*,dend第四章五、谋y编程!calculateyrealx,yread(*,*)xif(x.ge.0.and.x.lt.10)theny=xelseif(x.ge.10.and. x.lt.20)theny=x*x+1elseif(x.ge.20.and.x.lt.30)theny=x*x*x+x*x+1endifprint*,'x=',x,'y=',yend六、整除编程!zhengchuread(*,*)mif(mod(m,7).eq.0)print*,m,'能被7整除'if(mod(m,11).eq.0)print*,m,'能被11整除'if(mod(m,17).eq.0)print*,m,'能够被17相乘'if(mod(m,7).ne.0.and.mod(m,11).ne.0.and.mod(m,17).ne.0)print*,m,'无法被7,11,17相乘'end八、大小排序编程!programread(*,*)a,b,c,dif(a.lt.b)thentemp=aa=bb=tempendifif(a.lt.c)thentemp=aa=cc=tem pendifif(a.lt.d)thentemp=aa=dd=tempendifif(b.lt.c)thentemp=bb=cc=tempendifif(b.lt.d)thentemp=bb=dd=tempendifif(c.lt.d) thentemp=cc=dd=tempendifprint*,a,b,c,dend九、高程编程!programread(*,*)x,yif((x-2)**2+(y-2)**2.le.1)thenh=10elseif((x+2)**2+(y+2)**2.le.1)thenh=10elseif((x-2)**2+(y+2)**2.le.1)thenh=10elseif((x+2)**2+(y-2)**2.le.1)thenh=10elseh=0endifprint*,'h=',hend十、建筑规划编程!programread(*,*)x,yif(abs(x).le.10.and.abs(y).le.10)thenh=20elseif(abs(x).l e.20.and.abs(y).le.20)thenh=30elseif(abs(x).le.30.and.abs(y).le.30)thenh=50elseh=100endifprint*,'h=',hend一、求和编程!programsum=0sign=1do100,i=1,100,1sum=sum+sign*1.0/isign=sign*(-1)100continueprint*,'sum=',sumend结果为:0.6881718二、求和编程!programsum=0do100,i=1,20,1第五章sum=sum+1.0/i/(i+1)100continueprint*,'sum=',sumend结果为:0.9900995四、sinx函数编程!programsum=0read(*,*)xtemp=xsign=1i=1do100,while(abs(temp).gt.1e-10)sum=sum+sign*tempsign=sign*(-1)i=i+2temp=temp*x*x/i/(i-1)100continueprint*,sumend五、电阻编程!programread(*,*)r0,r1,r2temp=r0do100,i=1,3,1temp=(temp+r1)*r2/(temp+r1+r2)1 00continueprint*,tempend七、水解因子编程!programread(*,*)mprint*,m,'=1'20continuedo100,i=2,m,1if(mod(m,i).eq.0)thenprint*,'*',im=m/igoto20endif100continue。

Fortran作业、期末复习、西南交大

Fortran作业、期末复习、西南交大

编写两个子程序,分别求两个整数的最大公约数和最小公倍数,并编写主程序调用这两个子程序,输出结果program xinteger m,n,swrite(*,*)"请输入两个数:"read(*,*)m,ns=m*ncall acb(n,m)call asd(n,m,s)endsubroutine acb(n,m)integer tempif(m<n)thentemp=m;m=n;n=tempend ifdo while(mod(m,n)/=0)temp=n;n=mod(m,n);m=tempend dowrite(*,"('最大公约数为:'i4)") nreturnendsubroutine asd(n,m,s)if(m<n)thentemp=m;m=n;n=tempend ifdo while(mod(m,n)/=0)temp=n;n=mod(m,n);m=tempend dowrite(*,"('最小公倍数为:'i4)") s/nreturnend编写一个递归子程序,求Fibonacci数列某项的值。

并编写主程序调用该递归子程序,输出结果。

program xinteger n,tempwrite(*,*)"输入Fibonacci数列某项数,n大于三"read(*,*)ncall abc(n)endsubroutine abc(n)integer i,j,temp,a(n)a(1)=1a(2)=1do i=3,na(i)=a(i-1)+a(i-2)end dotemp=a(n)write(*,"('Fibonacci数列n项的值为:',i8)")tempreturnend1:输入10名学生的学号和一门课程的考试成绩,分别存入顺序文件和直接文件中。

fortran作业

fortran作业

1、反射波走时计算:program fansheboimplicit nonereal t,sum,t1,p,xinteger i,nreal,allocatable,dimension(:)::v,h !! Define arrays typewrite(*,*)'imput Model strata n'read(*,*) nallocate(v(n),h(n)) !!For dynamic array allocate memory write(*,102)'please imput',n,' level of velocity value'read(*,*)(v(i),i=1,n)write(*,102)'please imput ',n,' level of height value'read(*,*)(h(i),i=1,n)write(*,*)'please imput shot interval x'read(*,*) xsum=0.0call tp(x,p,n,v,h) !!carry out intrinsic procedure!! calculate the time Tdo i=1,nt1=h(i)/(v(i)*sqrt(1-(p**2)*(v(i)**2)))sum=sum+t1end dot=2*sum!!output the time Twrite(*,101) 'Light through', n ,'layer needs time:'write(*,100)t,'s'100 format(1x,f10.4,1x,A)101 format(1x,A,2x,i2,2x,A)102 format(1x,A,1x,i2,1x,A)deallocate(v,h)containssubroutine tp(x1,p1,n1,v1,h1) !! Define virtual parametersimplicit nonereal sum1,x1,x2,Y,y1,p1,p2,e,a,b,piinteger i,m,n1real,dimension(n1)::v1,h1 !!Difine virtual parameters arrayspi=3.1415927m=0b=5.0a=10.0do while(m<=1000) !!The most cycle 1000 times!! calculate Equation solutione=sin(pi*(a/180.0))p2=e/real(v(1))sum1=0.0do i=1,n1if(1-(p2**2)*(v1(i)**2)>0)theny1=(h1(i)*v1(i)*p2)/sqrt(1-(p2**2)*(v1(i)**2))sum1=sum1+y1else !!Gave the initial boundary value too big,reduce the initiala=a/2.0b=b/2.0end ifend dox2=2*sum1Y=x1-x2if(abs(Y)<=1.0e-6)then !!Get equation solution ,Jump out of circulationexitelseif(Y>0)thena=a+belsea=a-b/2.0 !! Stepping too big,reduce the stepb=b/2.0end ifend ifm=m+1end dop1=p2 !! return the resule p2 to p1write(*,103)'p=',p1 !! output p1103 format(1x,A,2x,f6.4)end subroutine tpend二、编写矩阵相乘的外部子程序,用于对任意大小的两个矩阵进行相乘。

二级FORTRAN程序设计上机题集

二级FORTRAN程序设计上机题集

二级FORTRAN程序设计上机题集上机题1======================================================================试题说明 :======================================================================补充编制fortran77程序prog1.for,其功能是计算并输出级数和1 1 (-1)^ns(n)=x - ─x^3 + ─x^5 - ... + ── x^(2n+1)3 5 2n+1直到s(n)-s(n-1)<0.000001为止。

其中x=0.7。

请编写该程序,最后运行程序。

其中部分程序与输出子程序writedat在程序中已经给出,不得修改。

======================================================================程序 :======================================================================write(*,100) s100 format(1x,'s=',e12.4)write(*,*)call writedat(s)endsubroutine writedat(s)open(10,file='bc01.out',status='new')write(10,100) s100 format(1x,e12.4)end======================================================================所需数据 :======================================================================@3 $bc01.out 001|.6107e+00#e上机题2=============================================================================== 试题说明 :补充编制fortran77程序prog1.for,其功能是计算并输出级数和1 1 1s(n)=1 + x + ─x^2 + ─x^3 +... + ─x^n2! 3! n!直到s(n)-s(n-1)<0.000001为止。

1998~2002年计算机考试二级FORTRAN(含答案)

1998~2002年计算机考试二级FORTRAN(含答案)

1998~2002年计算机二级考试FORTRAN部分1998年4月二级笔试试题FORTRAN部分21、下列选项中,所指定的FORTRAN表达式都是正确的是()①D=B * B - 4 * A * C②MOD(M,N).NE.O③.NOT.FALSE..OR..NOT..TRUE④A.GT.B.EQ.A.LT.C⑤π*R**2⑥3*E-2⑦A.GT.B.EQ..TRUE.⑧′123′//′ABC′A、①、②、③B、⑥、⑦、⑧C、④、⑤、⑥D、②、⑥、⑧22、执行下列程序,输入3456.789,则输出的结果为()。

READ(8 ,100) SWRITE( 8,100)S100 FORMAT(2F4.1)ENDA.345.6B.45.6C.456.D.* * *23、下列程序执行后输出( )DIMENSION M(5)READ(*,*)(M(I),I=1,5)DO 10 I=1,510 WRITE(*,22)(M(J),J=1,I)22 FORMAT(1X,3I3)ENDA、7B、8C、9D、524、FORTRAN 77规定,变量类型的优先顺序是()。

A、隐含约定(I-N规则)、Implicit说明、类型说明B、类型说明、隐含约定(I-N规则)、Implicit说明C、类型说明、Implicit说明、隐含约定(I-N规则)D、Implicit说明、类型说明、隐含约定(I-N规则)25、设T=.TRUE.F=.FALSE.下列表达式中值为.TRUE.的是( )。

A、.T.AND.F.AND.7.GE.4B、T.AND..NOT.T.AND.TC、((T.OR.F).AND.(T.AND.5.LE.4)).EQV.(.NOT.T.OR.(5.GE.1.AND.F))D、.NOT.(T.OR.F).NEQV.(T.AND.8-2.EQ.4)26 、下面循环语句执行后,SUM的值是()。

DO 10 I=1,5SUM=0DO 10 J=I,4,-110 SUM=SUM+I+JA、0.0B、19.0C.27.0D.76.027、下列语句中,正确的语句函数定义语句是()。

北航数值分析大作业第二题(fortran)

北航数值分析大作业第二题(fortran)

!计算A(r+1) DO I=1,N DO J=1,N A(I,J)=A(I,J)-W(I)*U(J)-U(I)*P(J) ENDDO ENDDO ENDIF ENDDO RETURN END
!***************符号函数子程序*****************! FUNCTION SGN(X) REAL(8) X IF(X>0) THEN SGN=1 ELSE IF(X<0) THEN SGN=-1 ELSE IF(X==0) THEN SGN=0 ENDIF END
DIMENSION A(N,N),A1(N,N),A2(N,N),C(2,N),Q(N,N),R(N,N),CR(N),CM(N)!C为存储特征值的数 组,1为实部,为虚部 REAL(8) A,A1,A2,C,Q,R,CM E=1E-12 L=1000 !精度水平 !迭代最大次数
OPEN(1,FILE='数值分析大作业第二题计算结果.TXT') DO I=1,N DO J=1,N IF(I==J) THEN A(I,J)=1.52*COS(I+1.2*J) ELSE A(I,J)=SIN(0.5*I+0.2*J) ENDIF ENDDO ENDDO A1=A WRITE(*,"('矩阵A为:')") WRITE(1,"('矩阵A为:')") DO I=1,N DO J=1,N WRITE(*,"(2X,E20.13,2X,\)") A(I,J) WRITE(1,"(2X,E20.13,2X,\)") A(I,J) ENDDO WRITE(*,"(' ')") WRITE(1,"(' ')") ENDDO !使用矩阵的拟上三角化的算法将矩阵A化为拟上三角矩阵A(n-1) CALL HESSENBERG(A,N) WRITE(*,"('拟上三角化后矩阵A(n-1)为:')") WRITE(1,"('拟上三角化后矩阵A(n-1)为:')") DO I=1,N DO J=1,N WRITE(*,"(2X,E20.13,2X,\)") A(I,J) WRITE(1,"(2X,E20.13,2X,\)") A(I,J) ENDDO WRITE(*,"('')") WRITE(1,"('')") ENDDO !计算对矩阵A(n-1)实行QR方法迭代结束后所得矩阵 A2=A CALL QRD(A2,N,Q,R)

fortran课后习题

fortran课后习题

5.3 program main implicit none integer nian,shouru real suijin read(*,*) nian read(*,*) shouru if (nian<50.and.nian>0) then if (shouru>=1000 .and. shouru<=5000) then suijin=shouru*0.1 end if if (shouru>5000) then suijin=shouru*0.15 end if if (shouru<1000) then suijin=shouru*0.03 end if else if (nian>50) then if (shouru>=1000 .and. shouru<=5000) then suijin=shouru*0.07 end if if (shouru>5000) then suijin=shouru*0.1 end if if (shouru<1000) then suijin=shouru*0.05 end if end if write(*,*)"应缴纳的税金金额为:" write(*,*) suijin end 5.4 program main implicit none integer day write(*,*) "请输入一个公元的年份:" read(*,*) day if (mod(day,4)==0) then
write(*,*)"一年有366天" else if(mod(day,100)==0.and.mod(day,400)==0) then write(*,*)"一年有366天" else write(*,*)"一年有365天" end if End 6.1 method one: program main implicit none integer counter do counter=1,5 write(*,*)"Fortran" end do end method two: program main implicit none integer counter counter=1 do while(counter<=5) write(*,*)"Fortran" counter=counter+1 end do end 6.2 program main implicit none integer counter integer::ans=0 counter=1 do while(counter<100) ans=ans+counter counter=counter+2 end do
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

FORTRAN第二次实习作业
2014301610307 -D-马莉1 、根据最新个人所得税计算方法,如下面的个税税率表。

工资、薪金所得,适用七级超额累进税率,税率为百分之三(3% )至百分之四十五(45% )。

3500 元为起征点,即只需要缴纳高于3500 元部分的个税。

请根据下表编程,通过给定个人收入,计算其所需邀纳的个税。

program jiaoshui
implicit none
integer m
real n
character i
100 print*,'应缴纳所得税是否含税,请回答Y或N:'
read*,i
print*,'请输入工资所得:'
read *,m
if(i=='Y')then
select case(m)
case(0:1500)
n=m*3/100-0
case(1501:4500)
n=m*10/100-105
case(4501:9000)
n=m*20/100-555
case(9001:35000)
n=m*25/100-1005
case(35001:55000)
n=m*30/100-2775
case(55001:80000)
n=m*35/100-5505
case default
n=m*45/100-13505
end select
elseif(i==’N’)then
select case(m)
case(0:1455)
n=m*3/100-0
case(1456:4155)
n=m*10/100-105
case(4156:7755)
n=m*20/100-555
case(7756:27255)
n=m*25/100-1005
case(27256:41255)
n=m*30/100-2775
case(41256:57505)
n=m*35/100-5505
case default
n=m*45/100-13505
end select
else
print*,"输入格式不正确,请重新输入:"
goto 100
endif
print*,n
End
2、编写程序求某天在当年中是第多少天,例如2010 年1 月10 号是2010 年的第10 天。

要求分别使用IF 、CASE结构来编写。

program tianshu2
implicit none
integer M1(12),i,y,m,d,sum,n
data M1/31,28,31,30,31,30,31,31,30,31,30,31/
sum=0
100 print *,"请输入日期格式例如:2010 10 23"
read 10,y,m,d
10 format(i4,1x,i2,1x,i2)
if(m>12.or.d>31)then
print*,"输入日期格式不对,请重新输入:"
goto 100
else
do i=1,m-1
sum=sum+M1(i)
enddo
sum=sum+d
if ((mod(y,4)==0.and.mod(y,4)/=0).or.mod(y,400)==0)then
sum=sum+1
endif
print *,y,'年',m,'月',d,'日是当年的第',sum,'天'
endif
End
3.编写程序,采用二分法求如下函数的解X3-2x-5=0, x∈[2,3]
module constant
real::a=2,b=3,c,er=1e-5
end module
program erfenfa
use constant
implicit none
real f
do while(abs(a-b)>1e-6)
c=(a+b)/2
if (f(c)*f(a)<0) then
b=c
else
a=c
End if
End do
print *,"f(x)=X3-2x-5=0的解x=",c,",最终f(x)=",f(c)
end
real function f(x) !定义函数
real x
f=x**3-2*x-5
End
4 、编写程序,对于自然数m ,n ,求其最大公约数和最小公倍数。

program gongyueshu
implicit none
integer m,n,i,j
100 print*,'请输入两个自然数:'
read *,m,n
if(m>n)then
i=n
else
i=m
endif
do while(mod(m,i)/=0.or.mod(n,i)/=0)
i=i-1
end do
print *,m,'和',n,'最大公约数是:',i
if(m<n)then
j=n
else
j=m
endif
do while(mod(j,m)/=0.or.mod(j,n)/=0)
j=j+1
end do
print *,m,'和',n,'最小公倍数是:',j
goto 100
End。

相关文档
最新文档