100% found this document useful (1 vote)
2K views

Assignment CBCP2103 (Computer Programming)

CBCP2103 COMPUTER PROGRAMMING Page Table of Contents ……………… 1 1.0 Question 1 1.1 Answer ……………… 2 ……………… 3 2.0 Question 2 2.1 Answer ……………….5 ……………… 5 3.0 Appendixes 3.1 References ……………… 7 Page 1 of 7 1.0 QUESTION 1 Write a C program to help a prospective borrower calculate the monthly payment for a loan. The program also prints the amortization (payoff) table to show the balance of the loan after each monthly payment. The program prompts the user for input, as shown in the following ex

Uploaded by

hamizona
Copyright
© Attribution Non-Commercial (BY-NC)
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
100% found this document useful (1 vote)
2K views

Assignment CBCP2103 (Computer Programming)

CBCP2103 COMPUTER PROGRAMMING Page Table of Contents ……………… 1 1.0 Question 1 1.1 Answer ……………… 2 ……………… 3 2.0 Question 2 2.1 Answer ……………….5 ……………… 5 3.0 Appendixes 3.1 References ……………… 7 Page 1 of 7 1.0 QUESTION 1 Write a C program to help a prospective borrower calculate the monthly payment for a loan. The program also prints the amortization (payoff) table to show the balance of the loan after each monthly payment. The program prompts the user for input, as shown in the following ex

Uploaded by

hamizona
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 7

CBCP2103

COMPUTER PROGRAMMING

Page
Table of Contents ……………… 1

1.0 Question 1 ……………… 2


1.1 Answer ……………… 3

2.0 Question 2 ……………….5


2.1 Answer ……………… 5

3.0 Appendixes ……………… 7


3.1 References

Page 1 of 7
1.0 QUESTION 1
Write a C program to help a prospective borrower calculate the monthly payment for a loan. The
program also prints the amortization (payoff) table to show the balance of the loan after each
monthly payment.

The program prompts the user for input, as shown in the following example:

Amount of the loan (Principa)? 10000.00


Interest rate per year (percent)? 12
Number of years? 10

The program then creates an information summary and amortization table.

Banks and financial institutions use different formulas to calculate the monthly payment for a
loan. For the purpose of this assignment, we use the following simple formula:

NM = (NY * 12)
IM = (IY / 12) / 100
P = (1 + IM)NM
Q = (P / (P – 1))
MP = (PR * IM * Q)

where
NY: Scheduled number of years to amortize the loan
NM: Scheduled number of months for the loan
IY: Interest rate per year (as a percentage)
IM: Interest rate / month (decimal)
PR: Principal (the amount of the loan)
P: The value of (1 + IM)NM
Q: The value of P / (P – 1)
MP: Monthly payment

Because of the approximation used in calculation, the value of the new balance at the end of the
last month may become nonzero. To prevent this, the last payment must be adjusted. It may be
less or greater than the other months. It must be calculated by adding the principal paid to the
interest paid for that month. The new balance at the end of the last month must be zero.

The following example shows the concept. The program has been run for a loan of RM 5000.00
at 11% interest rate for a period of 1 year. The input was

Amount of the loan (Principa)? 5000.00


Interest rate per year (percent)? 11
Number of years? 1

Run your program once with the test data shown in the following table.

Page 2 of 7
Amount Interest Years
Set 1 10,000.00 12 1
Set 2 5,000.00 10 2
Set 3 1,000.00 8 3

The output is shown in the following table.

The amount of the loan (principal): 5000.00


Interest rate/year (percent): 11.00
Interest rate/month (decimal): 0.009167
Number of years: 1
Number of months: 12
Monthly payment: 441.91

Month Old Monthly Interest Principal New


Balance Payment Paid Paid Balance

1 5000.00 441.91 45.83 396.08 4603.92


2 4603.92 441.91 42.20 399.71 4204.21
3 4204.21 441.91 38.54 403.37 3800.84
4 3800.84 441.91 34.84 407.07 3393.77
5 3393.77 441.91 31.11 410.80 2982.97
6 2982.97 441.91 27.34 414.57 2568.40
7 2568.40 441.91 23.54 418.37 2150.03
8 2150.03 441.91 19.71 422.20 1727.83
9 1727.83 441.91 15.84 426.07 1301.76
10 1301.76 441.91 11.93 429.98 871.78
11 871.78 441.91 7.99 433.92 437.86
12 437.86 441.91 4.01 437.86 0.00

Total amount paid: 5302.88

[Total marks: 50 Marks]


--------------------------------------------------------------------------------------------------------------------
1.1 ANSWER
/* Question 1: Calculate the Monthly Payment for a Loan */

#include <stdio.h>
#include <math.h>

int main()
{
double PR; /* Principal */
double IY; /* interest rate/year */
double IM; /* interest rate/month */
double MP; /* Monthly payment */
double OB; /* Old balance */
double IP; /* Interest paid */
double PP; /* Principal paid */
double NB; /* New balance */

Page 3 of 7
double P; /* Value of pow ((1+IM),NM) */
double Q; /* The value of P/(P-1) */
int NY; /* Number of year */
int NM; /* Number of month */
int month;

/* user input */

printf("Amount of the loan (Principal)? ");


scanf("%lf",&PR);

printf("Interest rate per year (percent)? ");


scanf("%lf",&IY);

printf("Number of years? ");


scanf("%d",&NY);

NM = (NY * 12);
IM = (IY / 12) / 100;
P = pow((1 + IM),NM);
Q = P/(P-1);
MP = (PR * IM * Q);

printf("\nThe amount of the loan (principal):\t%10.2lf\n",PR);

printf("Interest rate/year (percent):\t\t%10.2lf\n",IY);

printf("Interest rate/month (decimal):\t\t%14f\n",IM);

printf("Number of years:\t\t\t%7d\n",NY);

printf("Number of months:\t\t\t%7d\n",NM);

printf("Monthly payment:\t\t\t%10.2lf\n\n",MP);

OB=PR;

printf("Month Old Monthly Interest Principal New\n");


printf(" Balance Payment Paid Paid Balance\n\n");

for (month = 1; month<= NM; month++)


{
IP = OB * IM;
PP = MP - IP;
NB = OB - PP;

printf("%d\t%10.2lf\t%8.2lf\t%6.2lf\t%8.2lf\t%10.2lf\n",month,OB,MP,IP,PP,NB);
OB = NB;
}

Page 4 of 7
printf("\nTotal amount paid: %.2lf\n\n",NM*MP);
return 0;
}

--------------------------------------------------------------------------------------------------------------------
2.0 QUESTION 2
Write a program to compute the real roots of a quadratic equation (ax2 + bx + c = 0). The roots
can be calculated using the following formulas:

− b + b 2 − 4ac − b − b 2 − 4ac
x1 = and x2 =
2a 2a

Your program is to prompt the user to enter the constants (a, b, c). It is then to display the roots
based on the following rules:
a) If both a and b are zero, there is no solution
b) If a is zero, there is only one root (-c / b)
c) If the discriminate (b2 – 4ac) is negative, there are no real roots
d) For all other combinations, there are two roots

Test your program with the following data:

a b c
3 8 5
-6 7 8
0 9 -10
0 0 11

[Total marks: 40 Marks]

--------------------------------------------------------------------------------------------------------------------
2.1 ANSWER
/* Question 2: a program to compute the real roots of a quadratic equation (ax2 + bx + c = 0) */

#include <stdio.h>
#include <math.h>

int main()
{
double a,b,c;
double d;
double x1,x2;

printf("Enter constant a: ");


scanf("%lf",&a);
printf("Enter constant b: ");
scanf("%lf",&b);
Page 5 of 7
printf("Enter constant c: ");
scanf("%lf",&c);

printf("\n");
if(a==0 && b==0)
printf("No solution\n");
else if(a==0)
{

printf("There is only one root %.2lf\n",(-1)*c/b);

else

{
d= (b*b)-(4*a*c);
if(d<0)
{
printf("There are no real roots\n");
}
else
{
x1=(((-1)*b)+sqrt(d))/(2*a);
x2=(((-1)*b)-sqrt(d))/(2*a);
printf("There are two roots %.2lf, %.2lf\n",x1,x2);
}
}

return 0;
}

--------------------------------------------------------------------------------------------------------------------
ASSIGNMENT ENDS HERE

Page 6 of 7
3.0 Appendixes
3.1 References
• Roberge, J (1995). Data Structure in C++ A Laboratory Course. UK: D.C. Heath and
Company.
• Keong, W.C (2000). Data Structure With C. Kuala Lumpur: Sejana Publishing.
• Bakar, M.A. et al. (2009). CBCP2103 Computer Programming. Selangor: Pearson
Prentice Hall.
• Cprogramming.com.Your resource for C and C++ (”n.d.“). [Online]. Available:
http://www.cprogramming.com/. [2009, Nov,13].

Page 7 of 7

You might also like