Assignment 3
Assignment 3
Department of Mathematics
Assignment of Fortran
Course code:109
Submitted to
Dr.Faruque Ahmed
Professor –(co-ordinator), Dept.of Mathematics
Dr.Aminur Rahman khan
Professor, Dept.of Mathematics
Md.Abdur Rahman
Professor, Dept.of Mathematics
S M Mahmudul Hasan
Assistant Professor, Dept.of Mathematics
Submitted by
Roll: 41
Registration no-20220154647
Session: 2021-2022
Batch: 51
Date of submission
20th August, 2023
Question-1:Write a Fortran program to calculate the area of a triangle,circle and rectangle.
Solve:
!Area of a triangle
program area
implicit none
real::a,b,c,s,r
print*,"Input the value of sides:"
read(*,*)a,b,c
if(a+b>c.and.b+c>a.and.c+a>b)then
write(*,*)"a,b,c form a triangle."
else
print*,"Don't form a triangle."
stop
end if
s=(a+b+c)/2
r=Sqrt(s*(s-a)*(s-b)*(s-c))
print*,"The area is:",r
end program area
!Area of a Circle
program area
implicit none
real::r,a
real,parameter::Pi=3.1416
print*,"Input the value of radius"
read(*,*)r
a=Pi*r**2
print*,"The area is:",a
end program area
!Area of a Rectangle
program area
implicit none
real::l,w,a
print*,"Input the value of length:"
read(*,*)l
print*,"Input the value of width:"
read(*,*)w
a=l*w
print*,"The area is:",a
end program area
Solve:
program temp
implicit none
real::c,f,c2f,f2c
!Celsius to Fahrenheit
write(*,*)"Enter the Celsius value:"
read(*,*)c
c2f=((9*c)/5)+32
write(*,*)"The converted Fahrenheit temperature is:",c2f
!Fahrenheit to Celcius
write(*,*)"Enter the Fahrenheit value:"
read(*,*)f
f2c=5*(f-32)/9
write(*,*)"The converted Celsius temperature is:",f2c
end program temp
Question-3:Write a Fortran program to determine whether a given year is leap year or not.
Solve:
program leap_year
implicit none
integer::Year
print*,"Enter the year:"
read(*,*)Year
if(mod(year,400)==0)then
print*,"This year is leap year"
else if(mod(year,100)/=0 .and. mod(year,4)==0)then
print*,"This year is leap year."
else
print*,"Not a leap year."
end if
end program leap_year
Solve:
Question 5(a):1+2+3+...+n
program a
implicit none
integer::sum,n,i
write (*,*)"insert the value of n"
read(*,*)n
sum=0
do i=1,n,1
sum=sum+i
end do
write (*,*)"The required summation is=",sum
end program a
program b
implicit none
integer::sum,n,i
write (*,*)"insert the value of n"
read(*,*)n
sum=0
do i=1,n,1
sum=sum+i**2
end do
write (*,*)"The required summation is=",sum
end program b
Question 5(c):Cube summation
program c
implicit none
integer::sum,n,i
write (*,*)"insert the value of n"
read(*,*)n
sum=0
do i=1,n,1
sum=sum+i**3
end do
write (*,*)"The required summation is=",sum
end program c
Question 5(d):1+3+5+...+2n-1;
program d
implicit none
integer::sum,n,i
write (*,*)"insert the value of n"
read(*,*)n
sum=0
do i=1,n,2
sum=sum+i
end do
write (*,*)"summation is=",sum
end program d
Question 5(e):2+4+6+...2n;
program e
implicit none
integer::sum,n,i
write (*,*)"insert the value of n"
read(*,*)n
sum=0
do i=2,n,2
sum=sum+i
end do
write (*,*)"summation is=",sum
end program e
Question-6:Write a Fortran program to identify the nature of the conic.
Solve:
program identify_conic
implicit none
integer::a,b,c,f,g,h
real::p,q
write(*,*)"Enter the value of a,b,h:"
read(*,*)a,b,h
p=(a*b)-h**2
write(*,*)"ab-h2=",p
write(*,*)"Enter the value of f,g,c:"
read(*,*)f,g,c
q=(a*b*c)+(2*f*g*h)-(a*f**2)-(b*g**2)-(c*h**2)
write(*,*)"Discriminant=",q
if(q/=0)then
write(*,*)"It's a conic."
if(a==b.and.h==0)then
write(*,*)"This conic is a circle."
else if(p==0)then
write(*,*)"This conic is a parabola."
else if(p<0)then
write(*,*)"This conic is a hyperbola."
else if(p>0)then
write(*,*)"This conic is an ellipse."
end if
else
write(*,*)"It's not a conic."
end if
end program identify_conic
Question-7:Write a Fortran program to read the values g , f and c then calculate the radius and
centre of the circle.
Solve:
program radius
implicit none
real::g,f,c,h,k,radii
write(*,*)"Enter the value of g,f,c"
read(*,*)g,f,c
h=-g
k=-f
write(*,21)h,k
21 format(/,"Centre=","("f8.3,",",f8.3")")
radii=sqrt(g*g+f*f-c)
write(*,*)"The radii of the circle is=",radii
end program radius
Question-8:Write a Fortran program to display Fibonacci series.
Solve:
program fibonacci
implicit none
integer::n,i,a(100)
write(*,*)"Enter the numbers:"
read(*,*)n
a(1)=1
a(2)=1
do i=3,n,1
a(i)=a(i-1)+a(i-2)
end do
write(*,*)"The required fibonacci series is:"
write(*,21)(a(i),i=1,n)
21 format(//,1x,4i8)
end program fibonacci
Question-9:Write a Fortran program to display all prime numbers between any two integers.
Solve:
program prime
integer::n,m,i,j,count
character (len=20)::check, choose
do count =1,100,1
write (*,*)"insert highest range :"
read (*,*)m
write (*,*)"insert lowest range :"
read(*,*)n
write (*,10)m,n
10 format(//,1x,"the prime numbers between",1x,i5,1x,"and",i5,1x"are:")
do j=n,m,1
check ="yes"
do i=2,j-1,1
if (mod(j,i)==0)then
check ="no"
exit
end if
end do
if(check =="yes".and.j/=1)then
write (*,'(1x,i5)',advance ='no')j
end if
end do
write (*,20)
20 format(/,"if you want to try for another number then press (y) and for exit (n)")
read(*,*)choose
if(choose =="y")then
write (*,*)"try for another number! "
else
exit
end if
end do
end program prime
Question-10:Write a Fortran program to calculate the mean and sum of a list of integers.
Solve:
program sum_mean
implicit none
integer::i,n
real::sum,mean,a(100)
write (*,*)"Total numbers to be calculated:"
read(*,*)n
write(*,*)"Enter the value of the numbers"
do i=1,n,1
read(*,*)a(i)
end do
do i=1,n,1
sum=sum+a(i)
end do
write(*,21)sum
21 format(/1x"Summation is=",f8.3)
mean=sum/n
write(*,22)mean
22 format(/1x,"Mean=",f8.3)
end program sum_mean
Question-11:Write a Fortran program to read the total monthly consumption and compute the
monthly electric bill of a household. Given the rate of electricity is Tk. 2.50 per unit for first 100
units, Tk. 4.00 per unit for the next 100
units and Tk. 7.00 per unit for above 200 units.There is also a line rent of Tk. 120 per month and
15% tax on the total
number of unit consumed.
Solve:
program ElectricBill
implicit none
real::u,b,b1,tax
write(*,*)"Enter unit"
read(*,*)u
if(u>0.and.u<=100)then
b1=u*2.50
else if(u>100.and.u<=200)then
b1=250+((u-100)*4)
else
b1=250+400+((u-200)*7)
end if
tax=u*0.15
b=b1+120+tax
write(*,*)"Total electric bill:",b
end program ElectricBill
Solve:
program sale
implicit none
real::Commission, sales
write(*,*)"Insert the total sales="
read(*,*)sales
if(sales <100)then
Commission=0
write(*,*)"Commission =",Commission
else if(100<=sales.and.sales<500)then
Commission=(sales*10)/100
write(*,*)"Commission =",Commission
else if(sales>=500)then
Commission =50+((sales-500)*15)/100
write(*,*)"Commission =",Commission
end if
end program sale
Solve:
program Package_selling
implicit none
real::cost,weight
write(*,*)"Insert weight:"
read(*,*)weight
if(weight>0.and.weight<=100)then
write(*,*)" The package is accepted."
if(weight<=2)then
cost=15
write(*,*)"cost=",cost
else if(weight>2.and.weight<=70)then
cost=15+CEILING(weight-2)*5
write(*,*)"cost=",cost
else
cost=15+CEILING(weight-2)*5+15
write(*,*)"cost=",cost
end if
else
write(*,*)" The package is not accepted."
end if
end program Package_selling
Solve:
program table_form
implicit none
integer ::i,n,square, cube
real::sq_root,a(100)
write (*,*)"Insert the numbers to be calculated:"
read(*,*)n
write (*,5)
5 format (T5,2x,"Number",T15,2x,"SquareRoot",T30,2x,"Square",T40,2x,"Cube")
do i=1,n,1
sq_root=Sqrt(real(i))
square =i**2
cube=i**3
write (*,6)i,sq_root,square, cube
6 format (T4,2x,i5,T15,2x,f5.2,T20,2x,i6,T35,2x,i7)
end do
end program table_form
Question-15:Write a Fortran program to read and obtain marks of a course Between 1 and 100
and calculate the grade.
Solve:
program mark_list
implicit none
real::Mark
character(len=20)::gd
write (*,*)"Marks of a course"
read (*,*)mark
if(mark>=80.and.mark<100) then
gd="A+"
write (*,*)"The result of the course is=",gd
else if (mark>=75.and.mark<80)then
gd="A"
write (*,*)"The result of the course is=",gd
else if (mark>=70.and.mark<75)then
gd="A-"
write (*,*)"The result of the course is=",gd
else if (mark>=65.and.mark<70)then
gd="B+"
write (*,*)"The result of the course is=",gd
else if (mark>=60.and.mark<65)then
gd="B"
write (*,*)"The result of the course is=",gd
else if (mark>=55.and.mark<60)then
gd="B-"
write (*,*)"The result of the course is=",gd
else if (mark>=50.and.mark<55)then
gd="C+"
write (*,*)"The result of the course is=",gd
else if (mark>=45.and.mark<50)then
gd="C"
write (*,*)"The result of the course is=",gd
else if (mark>=40.and.mark<45)then
gd="D"
write (*,*)"The result of the course is=",gd
else if (mark<=40.and.mark>0)then
write (*,*)"F"
end if
end program
Question-16:Write a Fortran program to calculate the GPA for a first year student of
Mathematics Department 10 courses.
Solve:
program CalculateGPA
implicit none
integer:: Roll, credit(50), TotalCredit,i,n
real::cred_gpa(50),marks(50),gradep(50),TotalCred_gradep,GPA
character (len=50):: Name,CourseCode(50), gradechar(50)
if (marks(i)>=80) then
gradep(i)=4.0
gradechar(i)="A+"
else
gradep(i)=0.0
gradechar(i)="F"
end if
end do
write(*,10)
10 format(//,"Course Code", 5x, "credit",5x,"Obtained marks",5x,"Grade",5x,"Grade Point")
do i=1,n
write(*,20) CourseCode(i),credit(i),marks(i),gradechar(i),gradep(i)
20 format(2x,A8, 6x, i3,12x,f5.1,12x,A4,8x,f4.2)
end do
write(*,*)
write(*,*) "Calculate total credit and total credit times grade"
TotalCredit=0
TotalCred_gradep=0
do i=1,n
TotalCredit=TotalCredit+credit(i)
TotalCred_gradep=TotalCred_gradep+credit(i)*gradep(i)
end do
GPA=TotalCred_gradep/TotalCredit
write(*,30)
30 format(//,3x,"Roll",7x,"Name",10x"Course Code", 5x, "credit",5x,"Obtained
marks",5x,"Grade",5x,"Grade Point",5x,"GPA")
do i=1,n
if(i==1) then
write(*,40) Roll, Name,CourseCode(i),credit(i),marks(i),gradechar(i),gradep(i),GPA
40 format(2x,i7,5x,A10,5x,A8, 7x, i3,11x,f5.1,13x,A4,8x,f4.2,8x,f4.2)
else
write(*,50) CourseCode(i),credit(i),marks(i),gradechar(i), gradep(i)
50 format(29x,A8, 7x, i3,11x,f5.1,13x,A4,8x,f4.2)
end if
end do
end program CalculateGPA
Solve:
program qudratic_equation
implicit none
integer::a,b,c
real::D,x1,x2,realpart,imgpart
write(*,*)"Enter the value of a:"
read(*,*) a
write(*,*)"Enter the value of b:"
read(*,*) b
write(*,*)"Enter the value of c:"
read(*,*) c
if(a/=0)then
write(*,*)"This is quadratic Equation"
D=(b*b)-(4*a*c)
write(*,*)"Discriminant is=",D
if(D>0)then
write(*,*)"Roots are real and unequal"
x1=(-b+Sqrt(D))/(2*a)
x2=(-b-Sqrt(D))/(2*a)
write(*,21)x1,x2
21 format(2x,'root1=',f8.5,//2x,'root2=',f8.5)
else if(D==0)then
write(*,*)"Roots are real and equal"
x1=(-b+Sqrt(D))/(2*a)
x2=(-b-Sqrt(D))/(2*a)
write(*,21)x1,x2
else
write(*,*)"Roots are imaginary"
realpart=(-b)/(2*a)
imgpart=sqrt(-D)/(2*a)
write(*,22) realpart,imgpart,realpart,imgpart
22 format(2x'root1=',f8.3, 1x,'+',f8.3,'i',//,2x,'root2=',f8.3, 1x,'-',f8.3,'i')
end if
else
write(*,*)"This is not a Quadratic Equation"
end if
end program qudratic_equation
Solve:
program Ascending_descending
implicit none
integer:: n,i,j
real:: a(100),temp
write(*,*) "Total number to be listed:"
read(*,*) n
write(*,*) "Insert the numbers:"
do i=1,n,1
read(*,*) a(i)
end do
!finding ascending list
do i=1,n,1
do j=i+1,n,1
if (a(i)>a(j)) then
temp=a(i)
a(i)=a(j)
a(j)=temp
end if
end do
end do
write(*,*) "The numbers in ascending order are:"
do i=1,n,1
write(*,*) a(i)
end do
!finding descending list
do i=1,n,1
do j=1+i,n,1
if (a(i)<a(j)) then
temp=a(i)
a(i)=a(j)
a(j)=temp
end if
end do
end do
write(*,*) "The numbers in descending order are:"
do i=1,n,1
write(*,*) a(i)
end do
write(*,*) "The largest numbers is:", a(1)
write(*,*) "The smallest numbers is:", a(n)
end program ascending_descending
Question-19:Write a Fortran program to display the largest five numbers from a list of numbers.
Solve:
program largest
implicit none
integer:: n,i,j
real:: a(100),temp
write(*,*) "Total number to be listed:"
read(*,*) n
write(*,*) "Insert the numbers:"
do i=1,n,1
read(*,*) a(i)
end do
do i=1,n,1
do j=i+1,n,1
if (a(i)>a(j)) then
temp=a(i)
a(i)=a(j)
a(j)=temp
end if
end do
end do
write(*,21)a(n),a(n-1),a(n-2),a(n-3),a(n-4)
21 format(/,"The largest five numbers form the list are:",1x,f8.2,f8.2,f8.2,f8.2,f8.2)
end program largest
Question-20:Write a Fortran program to display the smallest four numbers from a list of
numbers.
Solve:
program smallest
implicit none
integer:: n,i,j
real:: a(100),temp
write(*,*) "Total number to be listed:"
read(*,*) n
write(*,*) "Insert the numbers:"
do i=1,n,1
read(*,*) a(i)
end do
do i=1,n,1
do j=i+1,n,1
if (a(i)<a(j)) then
temp=a(i)
a(i)=a(j)
a(j)=temp
end if
end do
end do
write(*,21)a(n),a(n-1),a(n-2),a(n-3)
21 format(/,"The smallest four numbers form the list are:",1x,f8.2,f8.2,f8.2,f8.2,f8.2)
end program smallest
Question-21:Write a Fortran program that will read and display a matrix and its transpose.
Solve:
program Matrix_display
implicit none
integer::i,j,m,n
!real::a(100,100), b(100,100), c(100,100)
real, dimension(100,100):: a,b,c
write(*,*) "Number of row:"
read(*,*) m
write(*,*) "Number of column:"
read(*,*) n
write(*,*) "Insert the value of Matrix A:"
do i=1,m,1
do j=1,n
read(*,*) a(i,j)
end do
end do
write(*,*) "Matrix A="
do i=1,m,1
write(*,10) (a(i,j),j=1,n)
10 format(10x,100(f4.1,5x))
end do
write(*,*)"Transposed matrix="
do i=1,m,1
write(*,10)(a(j,i),j=1,n)
end do
end program matrix_display
Question-22:Write a Fortran program to read and display a matrix and also calculate its
determinant.
Solve:
program deter
implicit none
real::A(20,20),k1,k2,c=1.
integer::i,j,n,m,k
write(*,*)'Number of row'
read(*,*)n
write(*,*)'Number of column'
read(*,*)m
if(m==n)then
write(*,*)"Determinant can be calculated."
write(*,*)"Enter elements:"
do i=1,n
do j=1,m
read(*,*)a(i,j)
end do
end do
write(*,*)'Matrix is:'
do i=1,n
write(*,21)(A(i,j),j=1,m)
21 format(2x,100(f8.2),5x)
end do
do k=1,n
k1=A(k,k)
do i=k+1,n
k2=A(i,k)/k1
do j=k,m
A(i,j)=A(i,j)-(k2*A(k,j))
end do
end do
end do
write(*,*)'Determinant is:'
do i=1,n
c=c*A(i,i)
end do
write(*,*)c
else
write(*,*)"Detarminant can't be calculated."
end if
end program deter
Question-23:Write a Fortran program to multiply two matrices and display the result.
Solve:
program Matrix_Multiplication
implicit none
integer::i,j,m,n,p,q,k
!real::a(100,100), b(100,100), c(100,100)
real, dimension(100,100):: a,b,c
write(*,*) "Number of row for matrix A:"
read(*,*) m
write(*,*) "Number of column for matrix A:"
read(*,*) n
write(*,*) "Insert the elements of Matrix A:"
do i=1,m,1
do j=1,n
read(*,*) a(i,j)
end do
end do
write(*,*) "Matrix A="
do i=1,m,1
write(*,5) (a(i,j),j=1,n)
5 format(10x,100(f4.1,5x))
end do
write(*,*) "Number of row for B Matrix:"
read(*,*) p
write(*,*) "Number of column for B Matrix:"
read(*,*) q
write(*,*) "Insert the elements of Matrix B:"
do i=1,p,1
do j=1,q,1
read(*,*) b(i,j)
end do
end do
write(*,*) "Matrix B="
do i=1,p,1
write(*,5) (b(i,j),j=1,q)
end do
if (n==p) then
write(*,*) "Calculate the Matrix C of order (m by q) by multiplying Matrix A and Matrix B:"
do i=1,m,1
do j=1,n
do k=1,n
c(i,j)=c(i,j)+a(i,k)*b(k,j)
end do
end do
end do
write(*,*) "Multiplication Matrix C="
do i=1,m,1
write(*,5) (c(i,j),j=1,q)
end do
else
write(*,*) "Matrix multiplication (AB) of A and B is not possible!"
end if
end program Matrix_Multiplication
Question-24:Write a program to read in the vertices of a triangle and decide whether the
triangle is a right triangle,isosceles triangle or an equilateral triangle.
Solve:
Program triangle
integer::i,j
real::x1,x2,x3,y1,y2,y3,a,b,c
write(*,*)"vertice 1"
read*,x1,y1
write(*,*)"vertice 2"
read*,x2,y2
write(*,*)"vertice 3"
read*,x3,y3
a=sqrt((x3-x2)**2+(y3-y2)**2)
b=sqrt((x1-x3)**2+(y1-y3)**2)
c=sqrt((x2-x1)**2+(y2-y1)**2)
else
end if
else
end if
end program
The End