1、fortran90例子例1、输入M个实数,将其相加,并输出其和。PROGRAM example_1Implicit noneInteger :n,mReal :t=0,a=0Read *,mDo Read *, a T=t+aN=n+1If (n=m) exitEnd doPrint*,tEnd program example_1例2、求I!的阶乘(I=4,8)。Function factor(n) result(fac_result) Implicit none Integer ,intent(in):n Integer,intent(out):fac_result Integer:I Fa
2、c_result=1 Do I=1,nFac_result=fac_result*I End doEnd function factorProgram example_2 Implicit none Integer :factor,s=0,I Do I=4,8 S=s+factor(i) End doPrint*,sEnd program example_2例3、输入一个数,判断他是否能被3整除,并输出相应的信息。Program judge Implicit none Integer : n,m Read*,n M=mod(n,3) Select case(m) IF (M= =0) THEN
3、 Case (0) Print*,yes Print*,YES Case default ELSE Print*,no Print*,NO End select END IF End program judge 例4、判断一个整数N是否为素数PROGRAM prime Implicit none Integer :n,I,m Read*,n M= sqrt(real(n) Do I=2,mIf(mod(n,i)= =0) exit End do If (Im) thenPrint*,yes ElsePrint*,no end ifend program prime例5、求N的阶乘PROGRAM
4、 example_5Implicit noneInteger:n,I=0,fac=1Read*,nDo while (I7) I=I+1 Fac=fac*IEnd doEnd program example_5例6、求出全部的水仙花数。(水仙花数是个三位数,其各位数字的立方和等于该数。)program example_6 implicit none integer :I,j,k,m,n ii: do I=1,9 jj: do j=1,9 kk: do k=1,9 m=I*100+j*10+k n=I*3+j*3+k*3 if (m= =n) print*,m end do kk end do
5、jj end do iiend program exaple_6例7、牛顿迭代法求方程X*4+4*X+1=0的根program example_7 implicit none integer :I=1,mreal:x0,x,eread*,x0,e,m (m控制迭代次数)do x=(-x0*x0-1)/4 if (abs(x-x0)=m) then Print*,not Exit End if End do If (I max_result) max_result=b if (c max_result) max_result=c end function max3program example_
6、11 use Exam_module real :aver3,max3 read*,a,b,c print*,aver3(),max3()end program example_11注意:(1)USE 模块名,ONLY :实体名例: use exam_module,only:a,b,此时C不再是共享变量,故C仍需通过虚实结合。 (2)use exam_module ,x-a 将模块中A与程序单元中变量X共享。例12、递归recursive function fac(n) result(fac_result) implicit none Integer ,intent(in):n Integer
7、,intent(out):fac_result If (n= =0) thenFac_result=1 Else Fac_result=fac(n-1)*n End ifEnd function facProgram example_12 Implicit none Interface Recursive function fac(n) result(fac_result) Integer ,intent(in):n Integer,intent(out):fac_resultEnd function facEnd interfaceInteger:nRead*,nPrint*,fac(n)E
8、nd program example_12例13、编一函数,求两数之和a)用外部过程实现program example_131 implicit none integer:a,b,sum read*,a,b call add(a,b,sum) print*,sumend program example_131subroutine add(a,b,sum)implicit noneinteger,intent(in):a,binteger,intent(out):sumsum=a+bend subroutine addb)用内部过程实现program exampl_132 implicit none integer :a,b,sum read*,a,b call add print*,sum contains subroutine addsum=a+b end subroutine add end program example_132
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1