0% found this document useful (0 votes)
9 views

Fortran Lesson5

Uploaded by

kumarroy07203010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Fortran Lesson5

Uploaded by

kumarroy07203010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

FORTRAN

Calculate the Sum of Natural Numbers:


program natural_nos

integer :: n, sum=0

write (*,*) 'Enter a positive integer : '

read (*,*) n

do i = 1, n

sum=sum+i

end do

write (*,*) 'Sum = ',sum

end program natural_nos


Find Factorial of a Number:
program factorial

integer :: n, fact=1

write (*,*) 'Enter a positive integer : '

read (*,*) n

do i = 1, n

fact=fact*i

end do

write (*,*) n,'factorial=',fact

end program factorial


Display Fibonacci Sequence:
program fibonacci

integer :: n, a=0, b=1, c

write (*,*) 'Enter the number of terms : '

read (*,*) n

write (*,*) 'Fibonacci Series : '

write (*,*) a

write (*,*) b

do i = 3, n

c=a+b

write (*,*) c

a=b

b=c

end do

end program fibonacci


Find GCD of two Numbers:
program GCD

integer :: num1, num2, ans, i=1

write (*,*) 'Enter two integer number : '

read (*,*) num1, num2

do while ((i .LE. num1) .AND. (i .LE. num2))

if ((Mod(num1,i) .EQ. 0) .AND. (Mod(num2,i) .EQ. 0)) then

ans=i

end if

i=i+1

end do

write (*,*) 'GCD is : ',ans

end program GCD


Find LCM of two Numbers:
program LCM

integer :: num1, num2, ans, gcd, i=1

write (*,*) 'Enter two integer number : '

read (*,*) num1, num2

do while ((i .LE. num1) .AND. (i .LE. num2))

if ((Mod(num1,i) .EQ. 0) .AND. (Mod(num2,i) .EQ. 0)) then

gcd=i

end if

i=i+1

end do

ans=(num1*num2)/gcd

write (*,*) 'LCM is : ',ans

end program LCM

You might also like