FORTRAN PROGRAMMING EXAMPLE
1. Series 1/1 + 1/2 + 1/3 + 1/4 +….. up to n terms.
program series
real n,i,sum
sum=0
write(*,*)'Enter the value of n'
read(*,*)n
i=0
12 if([Link].n)goto 2
i=i+1
sum=sum+1/i
goto 12
2 write(*,*)'Sum is :',sum
stop
end
2. Series 1/12 + 1/22 + 1/32 + 1/42 +….. Up to n terms. [IOE 2066]
program series
real n,i,sum
sum=0
write(*,*)'Enter the value of n'
read(*,*)n
i=0
12 if([Link].n)goto 2
i=i+1
sum=sum+1/(i*i)
goto 12
2 write(*,*)'Sum is :',sum
stop
end
3. FORTRAN program to check whether a given number is palindrome or not.
program palindrome
integer n,n_c,rev,rem
write(*,*)'Enter number:'
read(*,*)n
n_c=n
rev=0
1 if([Link].0)goto 2
rem=mod(n,10)
rev=rev*10+rem
n=n/10
goto 1
2 if(n_c.[Link])goto 3
write(*,*)'Not Palindrome'
goto 4
3 write(*,*)'Palindrome'
4 stop
end
4. FORTRAN program to check whether a given number is prime or not.
program prime
integer num
write(*,*)'Enter num:'
read(*,*)num
do 1 j=2,num-1,1
if(mod(num,j).eq.0)then
goto 3
endif
1 continue
goto 2
3 write(*,*)'Not prime:'
goto 6
2 write(*,*)'Prime:'
6 stop
end
5. FORTRAN program to find the factorial of given number.
program factorial
integer n,fact,i
i=1
fact=1
write(*,*)'Enter n:'
read(*,*)n
1 if([Link].n)goto 2
i=i+1
fact=fact*i
goto 1
2 write(*,*)'Factorial is:',fact
stop
end
6. FORTRAN program to check whether a given year is leap or not. [IOE 2068]
program leap_year
integer y
write(*,*)'Enter year:'
read(*,*)y
if((mod(y,4).[Link](y,100).ne.0).or.(mod(y,400).eq.0))goto 1
write(*,*)'Not leap year'
goto 2
1 write(*,*)'Leap year'
2 read(*,*)y
stop
end
7. FORTRAN program to implement quadratic equation ax2 + bx + c=0.
program quadratic
real a,b,c,r1,r2,d,rp,ip
write(*,*)'Enter values of a,b and c'
read(*,*)a,b,c
if(b*b-4*a*c)10,20,30
10 rp=-b/(2*a)
ip=sqrt(abs(b*b-4*a*c))/(2*a)
write(*,*)'Imaginary roots are:',rp,'+i',ip,'and',rp,'-i',ip
goto 80
20 r1=-b/(2*a)
write(*,*)'Roots are real and equal which are:',r1,r1
goto 80
30 r1=(-b+sqrt(b*b-4*a*c))/(2*a)
r2=(-b-sqrt(b*b-4*a*c))/(2*a)
write(*,*)'Roots are real and unequal and which are:',r1,r2
80 stop
end
8. FORTRAN program to to evaluate the series sin(x)= x – x3/3! + x5/5! - x7/7!……..
program sine_series
real deg,rad,sum,term,i
sum=0
i=0
write(*,*)'Enter value in degree:'
read(*,*)deg
rad=deg * 3.141592/180
term = rad
2 if(abs(term).lt.0.00001)goto 1
i=i+2
sum = sum + term
term = -term * rad * rad / (i*(i+1))
goto 2
1 write(*,*)'Sin(', deg, ')=',sum
stop
end
9. Write a program in Fortran to take a position and a number and insert this number on
this position inside an array containing n elements. [IOE 2067]
program insert_on_array
integer a(20),n,k,pos,num
write(*,*)'How many number in array?'
read(*,*)n
write(*,*)'Enter',n,' numbers'
do 1 k=1,n,1
read(*,*)a(k)
1 continue
write(*,*)'Enter position where number to be inserted'
read(*,*)pos
write(*,*)'Enter number that is to be inserted'
read(*,*)num
do 2 k=n+1,pos,-1
a(k)=a(k-1)
2 continue
a(pos)=num
write(*,*)'Data after inserting number'
do 3 k=1,n+1,1
write(*,*)a(k)
3 continue
stop
end
10. FORTRAN program to sort array in ascending order.
program ascending
integer a(20),k,l,n,temp
write(*,*)'How many data in array?'
read(*,*)n
write(*,*)'Enter',n,' number'
do 1 k=1,n,1
read(*,*)a(k)
1 continue
do 2 k=1,n,1
do 3 l=k+1,n,1
if(a(k).gt.a(l))then
temp=a(k)
a(k)=a(l)
a(l)=temp
endif
3 continue
2 continue
write(*,*)'Sorted array is:'
do 4 k=1,n,1
write(*,*)a(k)
4 continue
stop
end