In: Computer Science
C language <stdio.h> (Decisions only) (else if and Switch statements)
A bank wants an application to help in the loan approval process and has asked us to create it for them. Write a program to ask the user for the loan amount, the annual interest rate (as a percentage, not as a decimal), the length of the loan (in years), and the gross monthly income of the applicant. Validate the inputs per below, and if invalid, inform the user and end the program:
Loan amount must be greater than $0.00
Annual interest rate must be greater than 1.0% and less than 20.0%
Loan duration must be between 1 year and 30 years
Monthly income must be greater than $0.00
After all the inputs have been validated, calculate and display the monthly loan payment and the total interest for the loan based on the following formulas:
payment = p * r / (1 – (1 + r)-n )
total interest = n * payment – p
where:
p = the loan amount
r = monthly interest rate (annual rate / 12)
n = loan duration (in months)
Finally, determine if the applicant qualifies for the loan.
If the loan payment is less than or equal to 20.0% of the monthly income, they definitely qualify
If the loan payment is greater than 20.0% and less than or equal to 25.0% of the monthly income, it will be tight, but they qualify
If the loan payment is greater than 25.0% and less than or equal to 30.0% of the monthly income, they qualify (but the loan probably shouldn’t be approved)
If the loan payment is more than 30.0% of the monthly income, they do not qualify
Create constants for any numbers used that do not change (0, 1, 12, 20, 30, & 100) and the loan qualification levels (20.0%, 25.0%, & 30.0%), and use the constants in the validation conditions and error messages, in conversions from percentages to decimal (and vice versa) and conversions from years to months.
Display all dollar amounts with a dollar sign and 2 decimal places, and all percentages to 1 decimal place followed by a percent sign.
Example Run #1: (bold type is what is entered by the user)
Enter the amount of the loan: $100000.00
Enter the annual interest rate (8.0 = 8.0%): 4.0
Enter the length of the loan (in years): 30
Enter your monthly gross income: $3000.00
The monthly payment is: $xxx.xx (~$480.00)
The total interest is: $xxxxx.xx (~$71,870.00)
The monthly payment is less than or equal to 20.0% of your gross monthly income.
You are definitely approved for the loan!
Example Run #2:
Enter the amount of the loan: $200000.00
Enter the annual interest rate (8.0 = 8.0%): 4.0
Enter the length of the loan (in years): 30
Enter your monthly gross income: $3000.00
The monthly payment is: $xxx.xx
The total interest is: $xxxxxx.xx
The monthly payment must be less or equal to 30.0% of your gross monthly income.
I’m sorry – You do not qualify for the loan.
Example Run #3:
Enter the amount of the loan: $-50000
The mortgage amount must be greater than $0.00.
The example runs show EXACTLY how your program input and output will look.
************BankLoan.c**************
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
const int ze = 0;
const int o = 1;
const int tw = 12;
const int twe = 20;
const int th = 30;
const int hu = 100;
const float twper =20.0;
const float tw5per =25.0;
const float thper =30.0;
float p,ar,r,ginco, n,ylen,tot_in,pay;
int flag;
printf("Enter the amount of the loan: $");
scanf("%f",&p);
if(p<=ze)
{
printf("Loan amount must be greater than $0.00");
exit(0);
}
printf("\nEnter the annual interest rate (8.0 = 8.0%):");
scanf("%f",&ar);
if(ar<o && ar>twe)
{
printf("Annual interest rate must be greater than 1.0% and less
than 20.0%");
exit(0);
}
r=ar/(tw*hu);
printf("\nEnter the length of the loan (in years):");
scanf("%f",&ylen);
if(ylen<o && ylen>th)
{
printf("Loan duration must be between 1 year and 30 years");
exit(0);
}
n=ylen*tw;
printf("\n Enter your monthly gross income: $");
scanf("%f",&ginco);
if(ginco<=ze)
{
printf("Monthly income must be greater than $0.00");
exit(0);
}
pay=(p*r*pow(r+1,n))/(pow(r+1,n)-1);//replace the given formula
with this one and it will work
tot_in=n*pay-p; //this is coreect
printf("The monthly payment is: $ %0.2f",pay);
printf("\n The total interest is: $ %0.2f",tot_in);
float j=twper/hu;
float k=tw5per/hu;
float l=thper/hu;
//for wach logic we set a value for flag
if(pay<=j*ginco)
{
flag=1;
}
else if(pay>j*ginco && pay<=k*ginco)
{
flag=2;
}
else if(pay>k*ginco && pay<=l*ginco)
{
flag=3;
}
else if(pay>l*ginco)
{
flag=4;
}
//using the flag values in switch case
switch(flag)
{
case 1:
printf("\n The monthly payment is less than or equal to 20.0% Of
your gross monthly income.");
printf("\n You are definitely approved for the loan!");
break;
case 2:
printf("\nThe monthly payment is greater than 20.0% and less than
or equal to 25.0% of the monthly income.");
printf("\n It will be tight, but you qualify");
break;
case 3:
printf("\n The monthly payment is greater than 25.0% and less than
or equal to 30.0% of the monthly income.");
printf("\n You qualify (but the loan probably shouldn’t be
approved)");
break;
case 4:
printf("\n The monthly payment must be less or equal to 30.0% of
your gross monthly income.");
printf("\n I’m sorry – You do not qualify for the loan.");
break;
}
}
**********************If this answer helps you please give like Thank YOU!!*********************
Updated answer fully working code