How to Prompt User for Desired Payment in Loan Payment Program?

In summary, this program loans you a set amount of money with an annual interest rate and monthly payment.
  • #1
Nicolaus
73
0
Can someone give him a head start re: the following Q:

We can make the program test itself by having it plug in the found root and evaluate the LHS (it should be zero). Complete the program by having it evaluate the LHS at x = root1.


! This program finds the roots of equations of the 2nd degree
! with coefficients a, b and c
program Equation
implicit none
real*4 a, b, c, delta
real*4 root1, root2
integer*1 count

! ----------------------------------------------Prompt and Input
print*, "Solves ax^2 + bx + c = 0"
print*, "Enter the coefficients a,b,c separated by commas..."
read*, a, b, c

! ----------------------------------------------Validate

! ----------------------------------------------Find delta
delta = b**2 - 4.* a * c

! ----------------------------------------------Determine Roots
if (delta .GT. 0.) then
root1 = (-b + sqrt(delta)) / (2. * a)
root2 = (-b - sqrt(delta)) / (2. * a)
count = 2
else if (delta .LT. 0.) then
count = 0
else
root1 = -b / (2. * a)
count = 1
end if

! ----------------------------------------------Show Results
if (count .EQ. 2) then
write(*,20) " Equation has two roots: ", root1, ", ", root2
else if (count .EQ. 1) then
write(*,20) " Equation has one root: ", root1
else
write(*,20) " Equation has no real roots!"
end if
20 format(1x, A, F10.4, A, F10.4)

! ----------------------------------------------Verify root1
if (count .GT. 0) then
print*, "Verifying the first root by computing the L.H.S.:"



end if

! ----------------------------------------------
end
 
Technology news on Phys.org
  • #2
Well, the next move is up to you. You have the coefficients a,b, and c and the roots, root1 and root 2. Write a statement which combines these values according to the formula ax^2+bx+c
 
  • #3
The previous Q asked me to modify the program under "Validate" so if a=0 then it stops the program from further computing, and now I need to output the root, if any, of the supplied 1st-degree equation. How do I modify the program to adopt this approach?


! This program finds the roots of equations of the 2nd degree
! with coefficients a, b and c
program Equation
implicit none
real*8 a, b, c, delta
real*8 root1, root2, x, LHS
integer*1 count

! ----------------------------------------------Prompt and Input
print*, "Solves ax^2 + bx + c = 0"
print*, "Enter the coefficients a,b,c separated by commas..."
read*, a, b, c

! ----------------------------------------------Validate
if (a .EQ. 0) then
print*, "This is not an equation of the 2nd degree!"
stop
end if

! ----------------------------------------------Find delta
delta = b**2 - 4.* a * c

! ----------------------------------------------Determine Roots
if (delta .GT. 0.) then
root1 = (-b + sqrt(delta)) / (2. * a)
root2 = (-b - sqrt(delta)) / (2. * a)
count = 2
else if (delta .LT. 0.) then
count = 0
else
root1 = -b / (2. * a)
count = 1
end if

! ----------------------------------------------Show Results
if (count .EQ. 2) then
write(*,20) " Equation has two roots: ", root1, ", ", root2
else if (count .EQ. 1) then
write(*,20) " Equation has one root: ", root1
else
write(*,20) " Equation has no real roots!"
end if
20 format(1x, A, F20.15, A, F20.15)

! ----------------------------------------------Verify root1
if (count .GT. 0) then
print*, "Verifying the first root by computing the L.H.S.:"
x = root1
LHS = a*x**2 + b*x + c
print*, "LHS = ", LHS
end if

! ----------------------------------------------
End
 
  • #4
What if you have two roots? Aren't you supposed to verify both?

If you have a linear equation instead of a quadratic, how would you solve it?
 
  • #5
Thanks, finished that one. Now, I wrote the following program and am wondering how I would write a code that prompts the user every 12 months to make a desired payment of up to 10% of what remains of the loan.

! ------- Loan Payment -------

program Borrow
implicit none

integer month
real loan, mPayment, interestRate

print*, "Enter the amount of the loan: "
read*, loan

print*, "Enter the annual interest rate (as a percent): "
read*, interestRate

print*, "Enter the monthly payment: "
read*, mPayment

print*, "Amount of the loan is: ", loan
print*, "Annual interest rate : ", interestRate
print*, "Your monthly payment is : ", mPayment

print*, "month loan"
month = 0
do while (loan .gt. 0)
loan = loan + loan*interestRate/100/12 - mPayment
month = month + 1
write(*,12), month, loan
12 format(1x, I5, F12.2)
end do

stop
end
 

1. How do I declare and initialize variables in Fortran?

In Fortran, variables are declared using the REAL, INTEGER, or CHARACTER keywords, followed by the variable name. For example, REAL :: x declares a variable x of type REAL. To initialize a variable, use the = symbol followed by the value. For example, x = 5.0 initializes x with the value of 5.0.

2. How do I write a loop in Fortran?

In Fortran, loops are written using the DO keyword, followed by the loop control variable, the starting and ending values, and an optional step size. For example, DO i=1, 10, 2 creates a loop where i starts at 1, ends at 10, and increments by 2 each iteration. The loop body is enclosed within END DO statements.

3. What is the difference between REAL and DOUBLE PRECISION in Fortran?

In Fortran, REAL and DOUBLE PRECISION are both data types for storing floating-point numbers. However, DOUBLE PRECISION has a higher precision than REAL and can store larger values. It is recommended to use DOUBLE PRECISION for calculations that require high precision.

4. How do I read and write data in Fortran?

In Fortran, data can be read from or written to a file using the READ and WRITE statements respectively. These statements take the variable or variables to be read or written, followed by the file unit number and other optional parameters. File unit numbers are used to identify different files in a program.

5. How do I debug my Fortran code?

Fortran provides several debugging tools, such as the PRINT statement for printing values to the screen, and the STOP statement for stopping the program at a specific point. Additionally, many Fortran compilers have built-in debugging features, such as breakpoints and step-by-step execution. It is also helpful to use proper coding practices, such as using descriptive variable names and adding comments to your code, to make it easier to identify and fix errors.

Similar threads

  • Programming and Computer Science
Replies
4
Views
621
  • Programming and Computer Science
Replies
12
Views
967
  • Programming and Computer Science
Replies
10
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top